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 OverflowBaeldung
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:...
Top answer 1 of 10
335
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)) {
2 of 10
242
Quite simple:
new File("/Path/To/File/or/Directory").exists();
And if you want to be certain it is a directory:
File f = new File("/Path/To/File/or/Directory");
if (f.exists() && f.isDirectory()) {
...
}
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
(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
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
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
Videos
02:14
How To Check If A File Or Directory Exists In Java? | Java File ...
03:46
How to check if a folder exists in Java? - YouTube
07:44
Java - How To Check If A File / Directory Exists Or Not In Java ...
03:43
exists method in java | How do you check if a file exists or not ...
09:29
How to check if a file exists (in download folder) from your App?
Java Tutorials | How to verify file exists in java
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.
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 ·
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 ·
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.
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 } ...
Reddit
reddit.com › r/c_programming › (linux) how to check if a directory exists?
r/C_Programming on Reddit: (Linux) How to check if a directory exists?
October 10, 2022 -
I was wondering if there's any way to check if a certain directory exists on any path for example:
/home/$USER/Documents
Top answer 1 of 2
8
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.
2 of 2
4
Keep an eye out for TOCTOU (time-of-check/time-of-use) issues. If you are doing this in prep for another operation, it is often better to just do the operation you want to do and gracefully handle errors than to do these kind of checks before the operation. But if you aren’t doing this as part of the preflight for another operation, then there’s nothing to worry about.
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
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: