To check if a file exists in Java you can use the following snippet.

Sample Java

String filePath = "C:\\Users\\Codesnippets\\Desktop\\Test.xml";
File file = new File(filePath);
if (file.exists()) {
	System.out.println("file " + filePath + " does exists!");
} 
else {
	System.out.println("file " + filePath +  " does not exists!");
}

To check if a file exists in java you can use the following snippet.

Sample Java

using java.io.File;

File file = new File(filePath);
if(file.exists() && !file.isDirectory()) 
{ 
      /* ... */ 
}

One thought on “How to check if a file exists in Java”

Leave a Reply