Ticker

6/recent/ticker-posts

Ad Code

Responsive Advertisement

How to check if a file exists or not in Java? File.exists() and File.isFile() methods in Java

How to check if a file exists or not in Java? File.exists() and File.isFile() methods in Java

How to check if file exists in on file system?

Sometime at runtime, you may have to find a file and make sure that exists before executing or running any command. You don’t want to get an exception while running program.

It’s very simple in Java to check if File exists.

There are two ways you could do that.

  1. File.exists() and !File.isDirectory()
  2. File.isFile()

Here is a complete Java tutorial which checks if file exist on file system or not.

Create Java class: CrunchifyCheckIfFileExists.java

package crunchify.com.java.tutorials;

import java.io.IOException;
import java.io.File;

/**
 * @author Crunchify.com
 * How to check if a file exists or not in Java? File.exists() and File.isFile() methods in Java.
 */

public class CrunchifyCheckIfFileExists {
        
        public static void main(String[] args) {
                
                crunchifyCheckUsingExists();
                crunchifyCheckUsingIsFile();
                
        }
        
        // Method-1: Check if a file exists using File.exists() method
        private static void crunchifyCheckUsingExists() {
                File crunchifyFile = new File("/Users/app/Download/crunchify.txt");
                File crunchifyFile2 = new File("/Users/app/Download/crunchify2.txt");
                try {
                        
                        // exists() - Tests whether the file or directory denoted by this abstract pathname exists.
                        
                        // isDirectory() - Tests whether the file denoted by this abstract pathname is a directory.
                        // Where it is required to distinguish an I/O exception from the case that the file is not a directory, or where several attributes of the
                        // same file are required at the same time, then the Files.readAttributes method may be used.
                        
                        crunchifyPrint(crunchifyFile.getCanonicalPath() + " \n==> Result: Does it exist? " + (crunchifyFile.exists() && !crunchifyFile.isDirectory()));
                        crunchifyPrint(crunchifyFile2.getCanonicalPath() + " \n==> Result: Does it exist? " + (crunchifyFile2.exists() && !crunchifyFile2.isDirectory()) + "\n");
                        
                        // getCanonicalPath(): Returns the canonical pathname string of this abstract pathname.
                        // A canonical pathname is both absolute and unique. The precise definition of canonical form is system-dependent.
                        
                } catch (IOException exception) {
                        exception.printStackTrace();
                }
        }
        
        // Simple Print Utility
        private static void crunchifyPrint(String print) {
                System.out.println(print);
        }
        
        // Method-2: Check if a file exists using File.isFile() method
        private static void crunchifyCheckUsingIsFile() {
                File crunchifyFile = new File("/Users/app/Download/crunchify.txt");
                File crunchifyFile2 = new File("/Users/app/Download/crunchify2.txt");
                
                try {
                        
                        // isFile() - Tests whether the file denoted by this abstract pathname is a normal file.
                        // A file is normal if it is not a directory and, in addition, satisfies other system-dependent criteria. Any non-directory file created by a Java application is guaranteed to be a normal file.
                        // Where it is required to distinguish an I/O exception from the case that the file is not a normal file,
                        // or where several attributes of the same file are required at the same time, then the Files.
                        
                        crunchifyPrint(crunchifyFile.getCanonicalPath() + " \n==> Result: Does it exist? " + crunchifyFile.isFile());
                        crunchifyPrint(crunchifyFile2.getCanonicalPath() + " \n==> Result: Does it exist? " + crunchifyFile2.isFile());
                        
                } catch (IOException exception) {
                        // IOException: Signals that an I/O exception of some sort has occurred. This class is the general class of exceptions produced by failed or interrupted I/O operations.
                        
                        exception.printStackTrace();
                }
        }
}

In above program, make sure to change file path and your file name.

IntelliJ IDEA console result

Just run above program as a Java Application and you should see result as below.

/Users/app/Download/crunchify.txt 
==> Result: Does it exist? true
/Users/app/Download/crunchify2.txt 
==> Result: Does it exist? false

/Users/app/Download/crunchify.txt 
==> Result: Does it exist? true
/Users/app/Download/crunchify2.txt 
==> Result: Does it exist? false

Process finished with exit code 0

Let me know if you face any issue running above Java program.

The post How to check if a file exists or not in Java? File.exists() and File.isFile() methods in Java appeared first on Crunchify.

Enregistrer un commentaire

0 Commentaires