How to check if a file exists in Java

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!");
}

Please Share us:
Please rate this snippet

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

Leave a Reply

How to check if a file exists in java

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()) 
{ 
      /* ... */ 
}

Please Share us:
Please rate this snippet

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

Leave a Reply