How can I check whether a file exists, before opening it for reading in Java (the equivalent of Perl\'s
-e $filename>
There are multiple ways to achieve this.
In case of just for existence. It could be file or a directory.
new File("/path/to/file").exists();
Check for file
File f = new File("/path/to/file");
if(f.exists() && f.isFile()) {}
Check for Directory.
File f = new File("/path/to/file");
if(f.exists() && f.isDirectory()) {}
Java 7 way.
Path path = Paths.get("/path/to/file");
Files.exists(path) // Existence
Files.isDirectory(path) // is Directory
Files.isRegularFile(path) // Regular file
Files.isSymbolicLink(path) // Symbolic Link