Using java.nio.file.Files:

Path path = ...;

if (Files.exists(path)) {
    // ...
}

You can optionally pass this method LinkOption values:

if (Files.exists(path, LinkOption.NOFOLLOW_LINKS)) {

There's also a method notExists:

if (Files.notExists(path)) {
Answer from Jesper on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java io › check if a file or directory exists in java
Check If a File or Directory Exists in Java | Baeldung
January 27, 2024 - Also, we can check if a Path is ... make sure if a file or directory exists in Java legacy IO world, we can call the exists() method on File instances:...
Discussions

Gradle Java Plugin - only add to -classpath if folder exists
Hi, I have the following project structure build.gradle folder/ subprojectA/ subprojectB/ src/test/java subprojectC/ where build.gradle is configured using subprojects like so: subprojects { apply plugin: 'java-library' sourceSets { a { compileClasspath += test.runtimeClasspath runtimeClasspath ... More on discuss.gradle.org
🌐 discuss.gradle.org
0
0
September 5, 2020
(Linux) How to check if a directory exists?
Call stat on the file path and see if it doesn't fail (no such file or no access most likely) and then check the flags with the S_ISDIR macro. More on reddit.com
🌐 r/C_Programming
6
5
October 10, 2022
Check if directory exists - Oracle Forums
By the following code I am checking an existance of a file in the database operating system: DROP DIRECTORY mydir; DEFINE my_os_directory='/tmp'; CREATE OR REPLACE DIRECTORY mydir AS '&&my_os_... More on forums.oracle.com
🌐 forums.oracle.com
August 28, 2014
Create file if it doesn't exist, as well as its folders?
Much more readable (imo), and works on any OS: from pathlib import Path output_file = Path("/some/path/file.txt") output_file.parent.mkdir(exist_ok=True, parents=True) output_file.write_text("some text") Would recommend using the following for writing to the file though (handles closing the file for you, stream handling etc.) with open(output_file, 'w') as file: file.write("some text") More on reddit.com
🌐 r/learnpython
26
64
August 5, 2022
🌐
Medium
medium.com › @AlexanderObregon › javas-files-exists-method-explained-11a2f15fe927
Java’s Files.exists() Method Explained | Medium
January 29, 2025 - Learn how Java's Files.exists() method checks file or directory existence, handles symbolic links, permissions, and validates user-provided paths.
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › io › check.html
Checking a File or Directory (The Java™ Tutorials > Essential Java Classes > Basic I/O)
The following code snippet verifies that a particular file exists and that the program has the ability to execute the file. Path file = ...; boolean isRegularExecutableFile = Files.isRegularFile(file) & Files.isReadable(file) & Files.isExecutable(file); Note: Once any of these methods completes, there is no guarantee that the file can be accessed. A common security flaw in many applications is to perform a check and then access the file.
🌐
How to do in Java
howtodoinjava.com › home › i/o › check if a file or directory exists in java
Check If a File or Directory Exists in Java
November 13, 2022 - To test to see if a file or directory exists, use the “exists()” method of the Java java.io.File class.
🌐
Edureka Community
edureka.co › home › community › categories › java › how do i check if a file exists in java
How do I check if a file exists in Java | Edureka Community
December 29, 2020 - 101325/how-do-i-check-if-a-file-exists-in-java · Home · Community · Categories · Java · How do I check if a file exists in Java · how do I turn a double in an array from 1000 to infinity Jul 9, 2024 · I have created a code in java for a flower shop and now my IDE is showing all the inputs are in red showing there is an error Jul 6, 2024 ·
Find elsewhere
🌐
Educative
educative.io › answers › how-to-check-if-a-file-or-folder-exists-in-java
How to check if a file or folder exists in Java
import java.nio.file.Paths; class Main { public static void checkFilePresent(Path path){ if (Files.exists(path)) { if (Files.isDirectory(path)) { System.out.println("It is a directory"); } else if (Files.isRegularFile(path)) { System.out.println("File test.txt present"); } } else { System.out.println("File not found "); } } public static void main( String args[] ) { String currentDir = "./"; String fileName1 = "test.txt"; Path path = Paths.get(currentDir + fileName1); checkFilePresent(path); String fileName2 = "write.txt"; path = Paths.get(currentDir + fileName2); checkFilePresent(path); } } Run ·
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-check-a-file-or-directory-exists-in-java
How to Check a File or Directory Exists in Java? - GeeksforGeeks
April 28, 2025 - You will discover how to test an existing file or directory in Java in this post. The java.io.File class provides useful methods on file. This example shows how to check a file's existence by using the file.exists() method of File class.
🌐
Techndeck
techndeck.com › java-code-to-check-if-folder-or-directory-exists-or-not
Java code to check if folder (or directory) exists or not - Techndeck
April 13, 2022 - Once the folder has been created, we are going to use ‘exists‘ and ‘isDirectory’ method of Java-IO library. They will test whether the folder is present or not and if it’s a directory or not, based on the output, it will throw ‘pass‘ or ‘fail‘ in return. ... Now, by using below line of code, we are deleting the created folder and performing the folder existence check again.
🌐
Index.dev
index.dev › blog › check-if-path-is-valid-java
How to Check if a Path is Valid in Java
January 17, 2025 - Existence: The file or directory exists (if necessary). Permissions: The user has permission to access the path. Java’s java.nio.file and java.io packages provide tools to validate paths effectively. ... Use the Paths.get() method to check if a path is syntactically valid.
🌐
Alvin Alexander
alvinalexander.com › java › java-file-exists-directory-exists
Java “file exists” testing | alvinalexander.com
August 4, 2024 - Putting it all together, here is an example that tests to see if a file exists, and whether or not that file is also a directory: import java.io.File; public class JavaFileDirectoryExistsExample { public static void main(String[] args) { // test "/var/tmp" directory File tmpDir = new File("/var/tmp"); boolean exists = tmpDir.exists(); if (exists) System.out.println("/var/tmp exists"); if (tmpDir.isDirectory()) System.out.println("/var/tmp is a directory"); // test to see if a file exists File file = new File("/Users/al/.bash_history"); exists = file.exists(); if (file.exists() && file.isFile()) { System.out.println("file exists, and it is a file"); } } }
🌐
Gradle
discuss.gradle.org › help/discuss
Gradle Java Plugin - only add to -classpath if folder exists - Help/Discuss - Gradle Forums
September 5, 2020 - Hi, I have the following project structure build.gradle folder/ subprojectA/ subprojectB/ src/test/java subprojectC/ where build.gradle is configured using subprojects like so: subprojects { apply plugin: 'java-library' sourceSets { a { compileClasspath += test.runtimeClasspath runtimeClasspath += test.runtimeClasspath } b { compileClasspath += test.runtimeClasspath runtimeClasspath += test.runtimeClasspath } ...
🌐
W3Docs
w3docs.com › java
How to check if a folder exists? | W3Docs
Here is an example of how you can use the exists method to check if a folder exists: ... import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class Main { public static void main(String[] args) throws Exception ...
🌐
Oracle
forums.oracle.com › ords › apexds › post › check-if-directory-exists-6315
Check if directory exists - Oracle Forums
August 28, 2014 - By the following code I am checking an existance of a file in the database operating system: DROP DIRECTORY mydir; DEFINE my_os_directory='/tmp'; CREATE OR REPLACE DIRECTORY mydir AS '&&my_os_...
🌐
JMP User Community
community.jmp.com › t5 › Discussions › How-to-check-if-a-folder-exists-and-create-if-not › td-p › 226994
How to check if a folder exists and create if not? - JMP User Community
September 24, 2019 - You are looking for the functions Directory Exists("path") and Create Directory("path"). Something like: path = "C:\foo\bar"; If(!Directory Exists(path), Create Directory(path), Print(path||" already exists.") );
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › system.io.directory.exists
Directory.Exists(String) Method (System.IO) | Microsoft Learn
// For File.Exists, Directory.Exists using System; using System.IO; using System.Collections; public class RecursiveFileProcessor { public static void Main(string[] args) { foreach(string path in args) { if(File.Exists(path)) { // This path is a file ProcessFile(path); } else if(Directory.Exists(path)) { // This path is a directory ProcessDirectory(path); } else { Console.WriteLine("{0} is not a valid file or directory.", path); } } } // Process all files in the directory passed in, recurse on any directories // that are found, and process the files they contain.
🌐
Baeldung
baeldung.com › home › scala io › check if a file or path exists in scala
Check If a File or Path Exists in Scala | Baeldung on Scala
April 28, 2026 - This method returns a boolean indicating if the file or directory denoted by the given pathname exists: scala> import java.io.File scala> File("/tmp/baeldung.txt").exists() val res0: Boolean = true scala> File("/tmp/unexisting_file").exists() val res1: Boolean = false scala> File("/tmp").exists() val res2: Boolean = true
🌐
LabEx
labex.io › tutorials › java-check-if-directory-exists-117392
Check if Directory Exists in Java | LabEx
You can use the File class in Java to check if a specified directory exists. You can use the isDirectory() method on a File object to check if it represents a directory.
🌐
Baeldung
baeldung.com › home › java › java io › check if a directory is empty in java
Check If a Directory Is Empty in Java | Baeldung
November 13, 2025 - In this quick tutorial, we’re going to get familiar with a few ways to find out if a directory is empty or not. As of Java 7, the Files.newDirectoryStream method returns a DirectoryStream<Path> to iterate over all the entries in the directory. So we can use this API to check if the given directory is empty or not: