Friday, April 24, 2020

How to check if a File exists in Java with Example

Oracle Java Tutorial and Material, Oracle Java Guides, Oracle Java Learning, Oracle Java Certification

Hello guys,  if you are working in Java for a couple of years then you have come across a scenario where you need to check if a file exists or not. This is often required while writing defensive code that writes or read from a file. While there is a method to check if a file exists in Java or not, it's not so straight forward to use because it returns true if a given path represents a file or a directory. Yes, I am talking about the File.exists() method of java.io package. This method is the key method to solve this problem but you need to keep in mind that it returns true if a given path exists and its either file or directory. If your requirement is not just to check if the path is valid but also it represents a file and you have permission to read the file then you also need to ensure that it's not a directory.

If you are looking for a code example to check if a file exists in Java or not then you have come to the right place. In this article, I will show you a Java program and we will see how to check if a File exists or not.

Its common requirement to first check if a file with input pathname exists or not and if there is no File than create a new file but before that, you need to do this check. Earlier, I have shared how to check if a file is empty or not and today, I will share whether a file exists or not.

While File.exists() looks alright to solve this problem there is another gem which many Java developers don't know, yes, I am talking about File.isFile() method which not only checks if a file exists but also ensures that its a file. This is actually a better way to check if a file exists in Java or not.

Java Example to check if File exists in Java or not


Following is my sample Java program to check if a File exists in directory or not. The absolute or relative path of a file is provided as input to program and it will tell you if that file exists in a file system or not.

import java.io.File;

/**
  * Simple Java program to check if a file exits or not.
  * While checking whether a file exist or not it's also important
  * to check if a path represent a File or Directory
  * because exists() method of File class return true if path points
  * to either directory or File in Java.
 
  */
public class FileTest{ 
 
    public static void main(String args[]) {

        // name of File to be check for existence
        String path = "C:/sample.txt"; //file and its exists
        //String path = "C:/temp"; //directory, there will be no output
     
        // creating file object from given path
        File file = new File(path);
     
        // check for if file exits and than this File object actually
           represent a file
        // because exists() method returns true even if path points to
           a directory
        if(file.exists() && !file.isDirectory()){
            System.out.println("File exits in File System
                       - Java check successful");
        }
     
        // isFile() from File class can check if its file
        // and it exists together     
        if(file.isFile()){
            System.out.println("Provided path is a file and it exists");
        }
         
    }
 
}
Output:
File exits in File System - Java check successful
Provided path is a file and it exists

That's all on How to check if a File or directory exists or not from Java program. You can use this code to check if a file exists or not before starting to write into it or open for any operation. Just keep in mind that the File.exists() method returns true if the given path is either file or directory so make sure that you also check for that condition as shown in this example.

A better way is to use the isFile() method which returns true if this instance is represented by a file and it exists.

Related Posts

0 comments:

Post a Comment