Determine if a File or Directory Exists in Java

This short Java tutorial shows how to check if a file or directory exists. First, you will need to create a File object for the file or directory. Once you have created a File object, you can determine if it exists.

File file = new File("c:/file.txt");
if (file.exists())
{
//File or directory exist
System.out.println("File does exist.");
}
else
{
//File or directory does not exist
System.out.println("File does not exist.");
}


0 comments:

Post a Comment