Java IOjava-io-examples

How to check if a file exists at the specified path

Posted On
Posted By admin

In this blog post, I will be explaining how you can check if a file exists at a specified path. Consider the following code snippet:


public class FileExists {

public static void main(String args[]){
String fileName = "C:/Test.txt";
File file = new File(fileName);
if(file.exists()){
System.out.println("File is present");
}
else
System.out.println("File is missing");
}
}

 

Here the File.exists method is used. This returns a true if a file with the specified name exists at the path specified, otherwise it returns a false.

If you run this code, you will get the following output (Assuming there is a file on C drive called test.txt):

File is present
If you like this post, please do let me know via the comments box below.  You can also connect with me via my Facebook Page or subscribe to my Youtube channel!

Related Post

leave a Comment