-2

So far I came with this solution, but I wonder if there is more efficient way to do this. Pseudo-code:

public static void main(String args[]){
boolean FileNotFound = false;
FileReader file = new FileReader("path");
   try (BufferedReader bReader = new BufferedReader(file){
   //nothing to execute here
   }catch (FileNotFoundException e) {
      FileNotFound = true; 
      }
if (FileNotFound) {
//generate the file
 } 
}
The Law
  • 344
  • 3
  • 20
  • 6
    `File.exists()`? –  Oct 02 '15 at 14:26
  • 1
    If you want to make sure it's a file, and not a directory, use [`File.isFile()`](http://docs.oracle.com/javase/7/docs/api/java/io/File.html#isFile%28%29) – tobias_k Oct 02 '15 at 14:27

1 Answers1

1

Just use

public static boolean fileExists(String path) {
    File file = new File(path);
    return file.exists();
}

And then just call it with

...
if(fileExists("path")) ...
...
xdevs23
  • 3,824
  • 3
  • 20
  • 33