-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
  • 307
  • 3
  • 20

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,595
  • 3
  • 21
  • 30