How can I check whether a file exists, before opening it for reading in Java (the equivalent of Perl\'s
-e $filename>
I know I'm a bit late in this thread. However, here is my answer, valid since Java 7 and up.
The following snippet
if(Files.isRegularFile(Paths.get(pathToFile))) {
// do something
}
is perfectly satifactory, because method isRegularFile returns false if file does not exist. Therefore, no need to check if Files.exists(...).
Note that other parameters are options indicating how links should be handled. By default, symbolic links are followed.
From Java Oracle documentation