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

0 votes
If I want to read a file and write the data into it, but how to check if the file exists or not?
Apr 18, 2018 in Java by parth295
• 4,600 points
160 views

3 answers to this question.

0 votes

To check whether the file exists or not, we have to import io package. And from this package, we have to import File Class.

import java.io.File;

File f = new File(filePathString); //filepath will be the directory path where you want to search a file
if(f.exists() && !f.isDirectory()) {
    System.out.println("File exists");
}
answered Apr 18, 2018 by Daisy
• 8,040 points
0 votes

Instead of using exists(), we can use isFile() because, will return true but will not allow you to open and read from it as a file.

new File("path/to/file.txt").isFile();
answered Jul 20, 2018 by Sushmita
• 6,850 points
0 votes

Using nio we can check whether file exists or not.

import java.nio.file.*;

Path path = Paths.get(filePathString);

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

if (Files.notExists(path)) {
  // file is not exist
}
answered Aug 14, 2018 by Sushmita
• 6,850 points

Related Questions In Java

0 votes
2 answers

How to find out a single character appears in String or not in Java?

You can use string.indexOf('s'). If the 's' is present in string, ...READ MORE

answered Aug 7, 2018 in Java by Sushmita
• 6,850 points
42 views
0 votes
2 answers

How to move or copy a file in Java

The new JAVA Specification Request 203 -NIO will ...READ MORE

answered 1 day ago in Java by Sirajul
• 18,490 points
27 views
0 votes
3 answers

How to read a Text File in Java?

You can use readAllLines and the join method to ...READ MORE

answered Jul 27, 2018 in Java by samarth295
• 2,190 points
77 views
0 votes
2 answers

How to read a text file in Java?

You can use Scanner class to read ...READ MORE

answered Aug 9, 2018 in Java by parth295
• 4,600 points
45 views
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,380 points

edited Nov 6, 2018 by Omkar 7,190 views
0 votes
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,050 points
1,816 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,050 points
4,739 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,050 points
414 views
0 votes
3 answers

How to check whether a string is empty or not? Is there a function for this?

str != null && str.length() != 0 alternatively str ...READ MORE

answered Sep 11, 2018 in Java by Sushmita
• 6,850 points
88 views
0 votes
2 answers

How to create a 2-D array in java?

int[][] multi = new int[5][]; multi[0] = new ...READ MORE

answered Jul 16, 2018 in Java by Daisy
• 8,040 points
34 views