How to Check a File or Directory Exists in Java? Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report One of the most frequent tasks carried out by a file system in an operating system is checking the existence of a directory or a file. In the form of library functions, most programming languages provide some level of file system accessibility. You will discover how to test an existing file or directory in Java in this post. Checking/Testing the presence of a directory 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. Java // Importing required libraries import java.io.*; public class GFG { public static void main(String[] args) throws IOException { // Creating object of file and providing it path File f = new File("C:\\Test\\GFG.txt"); // Check if the specified file // Exists or not if (f.exists()) System.out.println("Exists"); else System.out.println("Does not Exists"); } } Output: The above code sample will produce the following result (if the file "java.txt" exists in 'C:/Test' drive). Exists Comment More infoAdvertise with us Next Article How to Check a File or Directory Exists in Java? N nmkiniqw7b Follow Improve Article Tags : Java Technical Scripter Technical Scripter 2022 java-file-handling Practice Tags : Java Similar Reads How to check if a directory or a file exists in system or not using Shell Scripting? Shell scripting is really a powerful and dynamic way to automate your tasks. To test if a directory or file already exists in the system or not we can use shell scripting for the same along with test command. To proceed with the test script lets first check the test manual. To open a manual use the 2 min read How to find and open the Hidden files in a Directory using Java Pre-requisites: Java File Handling So far the operations using Java programs are done on a prompt/terminal which is not stored anywhere. But in the software industry, most of the programs are written to store the information fetched from the program. One such way is to store the fetched information 3 min read Files createTempDirectory() Method in Java with Examples The createTempDirectory() method of java.nio.file.Files Class is used to create a new directory using the given prefix and attributes. The given prefix acts as the name of the formed directory, may be null. The directory is set with given attributes. Based on the type of arguments passed, the Files 4 min read How to Scan All Mp3 Files In the Given Directory using Java? In this article, we will discuss how to scan all mp3 files and not only from a specific path for an android media player. We will use a recursive method for scanning files. Scan all folders and sort out all mp3 files using the File and FileFilter class. ImplementationJava import java.io.File; import 1 min read Moving a file from one directory to another using Java Java provides functions to move files between directories. Two ways to achieve this are described here. The first method utilizes Files package for moving while the other method first copies the file to destination and then deletes the original copy from the source. Using Files.Path move() method: R 2 min read Like