In Java, we can use Files.exists(path) to test if a file exists. The path can be a file or a directory. It is better to combine with !Files.isDirectory(path) to make sure that the existing file is not a directory.

 Path path = Paths.get("/home/mkyong/test/test.log");

  // file exists and it is not a directory
  if(Files.exists(path) && !Files.isDirectory(path)) {
      System.out.println("File exists!");
  }
Tagged:

Laisser un commentaire