Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if path or file exist in Scala

Tags:

scala

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

os.path.exists("/home") Out[4]: True 
like image 278
Muhammad Lukman Low Avatar asked Jan 17 '14 03:01

Muhammad Lukman Low


People also ask

How do I find the path to a file?

Click the Start button and then click Computer, click to open the location of the desired file, hold down the Shift key and right-click the file. Copy As Path: Click this option to paste the full file path into a document. Properties: Click this option to immediately view the full file path (location).

Is file a path?

A file path describes the location of a file in a web site's folder structure. File paths are used when linking to external files, like: Web pages. Images.

How do you check if a string is a directory?

To check if a string represents a path or a file programatically, you should use API methods such as isFile(), isDirectory().

Is path a directory?

A path is a slash-separated list of directory names followed by either a directory name or a file name. A directory is the same as a folder.


2 Answers

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 
like image 92
Vladimir Matveev Avatar answered Sep 24 '22 18:09

Vladimir Matveev


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

scala> new java.io.File("/tmp").exists res0: Boolean = true 
like image 42
Muhammad Lukman Low Avatar answered Sep 23 '22 18:09

Muhammad Lukman Low