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.
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.
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
How to validate for multiple files exists in directory?
Put the multiple files in an array and loop test it.
How to load a file dynamically without giving i’ts path? Can you please share the sample code
i need to delete a record from text file in java
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.
Sorry, unable to simulate it, try putting the file in another subfolder, for example, c:\test\ali.txt , I’m not sure about this. Maybe your root c drive has strict security?
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
@Rajeev You are wrong. After File file = new File(PATH); the file is not yet written to the filesystem.
Hi mkyong is it possible to determine whether a file is exist in different server file system
@mkyong Thanks for the good work! 🙂
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.
No, the legacy IO
new File(“c:\\mkyong.txt”)will NOT create a file.@Piyush – If the file is remote you wouldn’t be telling the program to look for the file in the root of C:
This wont always work. f.exist also returns true when it’s a directory
Try this
if(Files.exists(path) && !Files.isDirectory(path))What if the file is remote? The above code snippet may fail
If the remote is a web server, we can send an HTTP request and check the return code to test if the file exists.
You just saved me a headache!!