Which of the following is the method you can use to determine whether a file exists?

This post will discuss how to check if a file exists in Java.

When you are testing a file’s existence, three results are possible:

  • The file exists.
  • The file doesn’t exist.
  • The file’s status is unknown as the program does not have access to the file.

 
There are several ways to check for a file’s existence in Java. Each of the following solutions returns true if the file exists; false otherwise, when the file doesn’t exist or the file’s status is unknown.

1. Using File.exists() method

The idea is to use the File.exists() method to determine whether a file denoted by a specified pathname exists. This method returns true if the file exists; false otherwise.

Note that File.exists() returns true when your path points to a directory. So, it is recommended to call this method along with the File.isDirectory() method, which checks for a directory. This is demonstrated below:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

importjava.io.File;

classMain

{

    // Method to check if the file exists and is not a directory

    publicstaticboolean isFileExists(File file){

        return file.exists()&& !file.isDirectory();

    }

    publicstaticvoidmain(String[] args)

    {

        StringfilePath ="C:\\doc.txt";

        File file= newFile(filePath);

        if (isFileExists(file)){

            System.out.println("File exists!!");

        }

        else{

            System.out.println("File doesn't exist or program doesn't have access "+

                            "to the file");

        }

    }

}

Download Code

 
Please note that when operating on NFS-mounted volumes, java.io.File.exists sometimes returns false even though the file referenced actually does exist. See bug details here.

2. Using File.isFile() method

We have seen that File.exists() returns true if your path points to a directory. To explicitly avoid checking for a directory, it is recommended to use File.isFile() method instead of File.exists() method. The File.isFile() method tests whether the file denoted by the specified path is a normal file, i.e., the file is not a directory.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

importjava.io.File;

classMain

{

    // Method to check if the file exists

    publicstaticboolean isFileExists(File file){

        return file.isFile();

    }

    public staticvoidmain(String[]args)

    {

        StringfilePath="C:\\doc.txt";

        File file=new File(filePath);

        if (isFileExists(file)){

            System.out.println("File exists!!");

        }

        else{

            System.out.println("File doesn't exist or program doesn't have access "+

                        "to the file");

        }

    }

}

Download Code

3. Using NIO

From Java 7 onward, we can use java.nio.file.Files, which provides several static methods that operate on files, directories, or other types of files. To simply check for a file’s existence, we can use exists() and notExists() method of java.nio.file.Files class. The exists() method returns true if the file exists, whereas the notExists() method returns true when it does not exist. If both exists() and notExists() return false, the existence of the file cannot be verified. This can happen when the program does not have access to the file.

Note that Files.exists() returns true when your path points to a directory. So, it is recommended to use this method along with the Files.isDirectory() method, which checks the file for a directory. This is demonstrated below:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

importjava.nio.file.Files;

import java.nio.file.Path;

import java.nio.file.Paths;

classMain

{

    publicstaticvoid main(String[]args)

    {

        StringfilePath="C:\\doc.txt";

        Path path=Paths.get(filePath);

        booleanexists=Files.exists(path);

        booleannotExists= Files.notExists(path);

        boolean isDir=Files.isDirectory(path);

        if(isDir){

            System.out.println("File is a Directory");

        }

        elseif(exists){

            System.out.println("File exists!!");

        }

        elseif(notExists){

            System.out.println("File doesn't exist!!");

        }

        else{

            System.out.println("Program doesn't have access to the file!!");

        }

    }

}

Download Code

 
Alternatively, we can check if a path is a regular file (and not a directory) using the Files.isRegularFile() method:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

importjava.nio.file.Files;

import java.nio.file.Path;

import java.nio.file.Paths;

classMain

{

    publicstaticvoid main(String[]args)

    {

        StringfilePath="C:\\doc.txt";

        Path path=Paths.get(filePath);

        booleanexists=Files.isRegularFile(path);

        if(exists){

            System.out.println("File exists!!");

        }

        else{

            System.out.println("File doesn't exist!!");

        }

    }

}

Download Code

That’s all about determining whether a file exists in Java.

What kind of loop would be most appropriate if the number of iterations needed is known at run time?

For-loops are typically used when the number of iterations is known before entering the loop. For-loops can be thought of as shorthands for while-loops which increment and test a loop variable.

Is a while loop a pretest loop?

The while loop is known as a pretest loop, because it tests the boolean expression before it executes the statements in its body.

What is the term for a value that signals that we wish to stop a loop?

In computer programming, a sentinel value (also referred to as a flag value, trip value, rogue value, signal value, or dummy data) is a special value in the context of an algorithm which uses its presence as a condition of termination, typically in a loop or recursive algorithm.

Which of the following is the structure that causes a statement or set of statements to execute?

A repetition structure causes a statement or set of statements to execute repeatedly. Repetition structures are used to perform the same task over and over. A condition-controlled loop uses a true/false condition to control the number of times that it repeats.