How do I check if a file exists in Java

0 votes

If possible I'd prefer a real API call returning true/false as opposed to some "Call API to open a file and catch when it throws an exception which you check for 'no file' in the text", but I can live with the latter.

Dec 29, 2020 in Java by anonymous
• 8,910 points
58,340 views

4 answers to this question.

0 votes

Using java.io.File:

File f = new File(filePathString);
if(f.exists() && !f.isDirectory()) { 
    // do something
}
answered Dec 29, 2020 by Gitika
• 65,910 points
0 votes

To test to see if a file or directory exists, use the exists method of the Java File class, as shown in this example:

File tmpDir = new File("/var/tmp");
boolean exists = tmpDir.exists();

The existing method of the Java File class returns true if the file or directory exists, and false otherwise.

Hope this helps!

Join java online course to learn more.

Thanks!

answered Dec 29, 2020 by Nikita

edited Jun 22, 2023 by Khan Sarfaraz
0 votes

The exists() function is a part of File class in Java . This function determines whether the is a file or directory denoted by the abstract filename exists or not.The function returns true if the abstract file path exists or else returns false.

Function signature:

public boolean exists()

Syntax:

file.exists()

// Java program to demonstrate

// exists() method of File Class

  

import java.io.*;

  

public class solution {

    public static void main(String args[])

    {

  

        // Get the file

        File f = new File("F:\\program.txt");

  

        // Check if the specified file

        // Exists or not

        if (f.exists())

            System.out.println("Exists");

        else

            System.out.println("Does not Exists");

    }

}

Output:

Exists
answered Dec 29, 2020 by Carlos
0 votes

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.

Check file exist with exists() method
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.

Console

Temp file exists : true

answered Dec 29, 2020 by Rajiv
• 8,910 points

Related Questions In Java

0 votes
1 answer

How do I check if a string contains a substring in Java?

Hi@akhtar, The first and foremost way to check ...READ MORE

answered Dec 30, 2020 in Java by MD
• 95,440 points

edited Jul 5, 2023 by Khan Sarfaraz 50,606 views
0 votes
3 answers

How to check whether a file exists or not in Java?

Using nio we can check whether file ...READ MORE

answered Aug 14, 2018 in Java by Sushmita
• 6,910 points
3,529 views
0 votes
0 answers

How do I check if an array includes a value in JavaScript?

What is the shortest and most efficient ...READ MORE

Sep 28, 2022 in Java by Nicholas
• 7,760 points
418 views
0 votes
1 answer

How do I create a Java string from the contents of a file?

If you're looking for an alternative that ...READ MORE

answered Apr 19, 2018 in Java by Rishabh
• 3,620 points
927 views
0 votes
1 answer

Open file in Python

There is this code I used for ...READ MORE

answered Nov 16, 2018 in Python by Jino
• 5,810 points
555 views
0 votes
2 answers

File to byte[] in Java?

Example 1: Convert File to byte[] import java.io.IOException; import ...READ MORE

answered Dec 28, 2020 in Java by Reshma
515 views
0 votes
1 answer

How to append text to an existing file in Java?

Java 7+ If you just need to do ...READ MORE

answered Dec 30, 2020 in Java by Gitika
• 65,910 points

edited Jul 6, 2023 by Khan Sarfaraz 948 views
0 votes
1 answer

How to read all files in a folder from Java?

public void listFilesForFolder(final File folder) { ...READ MORE

answered Dec 30, 2020 in Java by Gitika
• 65,910 points
1,914 views
0 votes
1 answer

How do I determine whether an array contains a particular value in Java?

Arrays.asList(yourArray).contains(yourValue) Warning: this doesn't work for arrays of ...READ MORE

answered Dec 21, 2020 in Java by Gitika
• 65,910 points
976 views
0 votes
1 answer

How do I generate random integers within a specific range in Java?

Before Java 1.7, the standard way to ...READ MORE

answered Dec 22, 2020 in Java by Gitika
• 65,910 points
566 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP