How to check if a file exists in Java

In Java, we can use Files.exists(path) to test whether a file exists. The path can be a file or a directory. It is better to combine with !Files.isDirectory(path) to ensure the existing file is not a directory.


  Path path = Paths.get("/home/mkyong/test/test.log");

  // file exists and it is not a directory
  if(Files.exists(path) && !Files.isDirectory(path)) {
      System.out.println("File exists!");
  }

1. Files.exists(path) (NIO)

This example uses Files.exists(path) to test whether a file exists.

FileExist.java

package com.mkyong.io.file;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FileExist {

    public static void main(String[] args) {

        Path path = Paths.get("/home/mkyong/test/test.log");

        // check exists for file and directory
        if (Files.exists(path)) {

            if (Files.isRegularFile(path)) {
                System.out.println("File exists!");
            }
            if (Files.isDirectory(path)) {
                System.out.println("File exists, but it is a directory.");
            }

        } else {
            System.out.println("File doesn't exist");
        }

    }
}

1.2 If the file is a symbolic link or soft link, the Files.exists will follow the link by default. The Files.exists takes two arguments, we can pass a second LinkOption.NOFOLLOW_LINKS argument to tell the method stop to follow the symbolic link.


import java.nio.file.Files;
import java.nio.file.LinkOption;

    // do not follow symbolic link
    if(Files.exists(path, LinkOption.NOFOLLOW_LINKS)){
      //...
    }

1.3 There is a Files.notExists() to test the non-exists file.


  if(Files.notExists(path)){
      System.out.println("File doesn't exist");
  }

2. File.exists (Legacy IO)

The legacy IO java.io.File has a similar File.exists() to test whether a file or directory exists, but it has no support for symbolic links.

FileExist2.java

package com.mkyong.io.file;

import java.io.File;

public class FileExist2 {

    public static void main(String[] args) {

        File file = new File("/home/mkyong/test/");

        if(file.exists() && !file.isDirectory()){
            System.out.println("File exists!");
        }else{
            System.out.println("File doesn't exist");
        }

    }

}

Download Source Code

$ git clone https://github.com/mkyong/core-java

$ cd java-io

References

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

18 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Raymond
13 years ago

This wont always work. f.exist also returns true when it’s a directory

Piyush
13 years ago

What if the file is remote? The above code snippet may fail

Dilip
6 years ago

How to validate for multiple files exists in directory?

sai priya
9 years ago

How to load a file dynamically without giving i’ts path? Can you please share the sample code

Ramya Y
11 years ago

i need to delete a record from text file in java

ALI AKHTAR
11 years ago

In my C folder I made a file called ali.txt.

File f = new File(“c:\ali.txt”);
if(f.exists()){
System.out.println(“File existed”);
}else{
System.out.println(“File not found!”);
}

But it gives me File not found.

Lijo Jose
11 years ago

Can anyone help me to write a code for finding whether a new image file is added to a specified folder. And if a new file is found execute an action in java

ArcosBinary
12 years ago

@Rajeev You are wrong. After File file = new File(PATH); the file is not yet written to the filesystem.

Ravi
12 years ago

Hi mkyong is it possible to determine whether a file is exist in different server file system

Sanjay.bhodu
12 years ago

@mkyong Thanks for the good work! 🙂

Rajeev
12 years ago

Here is the right solution:
if (new File(“/Path/To/File/YourFileName.txt”).exists()){

}

If you are doing …
File f = new File(“c:\\mkyong.txt”);
and then checking the existence, it will always returns true because you are checking the existence after creating the file.

Dan
13 years ago

@Piyush – If the file is remote you wouldn’t be telling the program to look for the file in the root of C:

Christian
13 years ago

You just saved me a headache!!