How to check if path or file exist in Scala

匿名 (未验证) 提交于 2019-12-03 02:50:02

问题:

How do I check if a path / file exists in Scala similar to Python ? An example below:

os.path.exists("/home") Out[4]: True 

回答1:

Since Java 7 the better way would be

scala> import java.nio.file.{Paths, Files} import java.nio.file.{Paths, Files}  scala> Files.exists(Paths.get("/tmp")) res0: Boolean = true 


回答2:

Well, sorry I found the answer to my own question:

scala> new java.io.File("/tmp").exists res0: Boolean = true 


回答3:

you can use scala library as well :

scala.reflect.io.File(scala.reflect.io.Path("<actual file path>")).exists 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!