HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / I/O / How to check if file exists in Java

How to check if file exists in Java

In this tutorial, we will learn how can we test to check if file exists or a directory exist in given path in Java.

1. Check if file exists with File.exists() method

To test to see if a file or directory exists, use the “exists()” method of the Java java.io.File class, as shown here:

File tempFile = new File("c:/temp/temp.txt");
boolean exists = tempFile.exists();

If above method returns true then file or directory does exist, and otherwise does not exists.

import java.io.File;
import java.io.IOException;

public class TemporaryFileExample
{
   public static void main(String[] args)
   {
      File temp;
      try
      {
         temp = File.createTempFile("myTempFile", ".txt");
         
         boolean exists = temp.exists();
         
         System.out.println("Temp file exists : " + exists);
      } catch (IOException e)
      {
         e.printStackTrace();
      }
   }
}

Program Output.

Temp file exists : true

2. Files.exists() and Files.notExists() methods

Java NIO also provides good ways to test whether a file exists or not. Use Files.exists() method or Files.notExists() method for this.

final Path path = Files.createTempFile("testFile", ".txt");

Files.exists(path);     //true

//OR

Files.notExists(path);  //false

3. Check if file is readable, writable or executable

To verify that the program can access a file as needed, you can use the isReadable(Path), isWritable(Path), and isExecutable(Path) methods.

Java program to test a file if it is readable, writable and executable.

final Path path = ...;

Files.isReadable(path);

//OR

Files.isWritable(path);

//OR

Files.isExecutable(path);

That’s all for quick tip related to checking if a file or directory exists or does not exists in java. Along with test if program is allowed to append content to it by checking it’s writable attribute.

Happy Learning !!

Reference:

Java Docs

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Vijay

    August 7, 2020

    Hi All ,

    I need to check , for a particular parameter value exists in a file which exists in multiple virtual machines.

    Please help with the logic.

    Thanks,
    VIjay

  2. Himansu

    November 6, 2014

    Hi Lokesh,

    Can you elaborate more on how Files.notExists() is different from ! (Files.exists()) . Actually both of there source codes looks very much alike.

    Thanks,
    Himansu

    • Lokesh Gupta

      November 6, 2014

      You guess what.. you are absolutely right.. 🙂

Comments are closed on this article!

Search Tutorials

Java IO

  • Java IO Introduction
  • Java How IO works?
  • Java IO vs NIO
  • Java Create File
  • Java Write to File
  • Java Append to File
  • Java Read File
  • Java Read File to String
  • Java Read File to Byte[]
  • Java Make File Read Only
  • Java Copy File
  • Java Copy Directory
  • Java Delete Directory
  • Java Current Working Directory
  • Java Read/Write Properties File
  • Java Read File from Resources
  • Java Read File from Classpath
  • Java Read/Write UTF-8 Data
  • Java Check if File Exist
  • Java Create Temporary File
  • Java Write to Temporary File
  • Java Delete Temporary File
  • Java Read from Console
  • Java Typesafe input using Scanner
  • Java Password Protected Zip
  • Java Unzip with Subdirectories
  • Java Generate SHA/MD5
  • Java Read CSV File
  • Java InputStream to String
  • Java String to InputStream
  • Java OutputStream to InputStream
  • Java InputStreamReader
  • Java BufferedReader
  • Java FileReader
  • Java LineNumberReader
  • Java StringReader
  • Java FileWriter
  • Java BufferedWriter
  • Java FilenameFilter
  • Java FileFilter

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces