Java does not know when a file does NOT exist

Java knows when a file exists, when it prints "Found file", but when a file does not exist, it does NOT print "File not found"

    File file = new File(filePath, "Test_1.exe");

    if (file.exists()){
        System.out.println("File found");
    }else{
        System.out.println("File not found");
    }

does anyone know The file path is correct as I double-checked this. It is just strange that if the file does not exist, it will not print, but it will if it does.

I also tried if (! File.exists ()) was out of luck!

+3
source share
2 answers

Try

try
{
    File file = new File(filePath, "Test_1.exe");

     if (file.exists())
     {
          System.out.println("File found");
     }
     else
     {
            System.out.println("File not found");
      }
}
catch(SecurityException se)
{
     se.printStackTrace();
}
+2
source

Try using the full path to the file. eg:

File file = new File(filePath, "c:/temp/Test_1.exe");

From javadocs:

, , , - . java.io . user.dir , Java.

0

All Articles