Java IO Tutorial

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

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
18 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Raymond
11 years ago

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

Piyush
11 years ago

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

Dilip
4 years ago

How to validate for multiple files exists in directory?

sai priya
7 years ago

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

Ramya Y
9 years ago

i need to delete a record from text file in java

ALI AKHTAR
9 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
9 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
10 years ago

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

Ravi
10 years ago

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

Sanjay.bhodu
10 years ago

@mkyong Thanks for the good work! 🙂

Rajeev
10 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
10 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
11 years ago

You just saved me a headache!!