Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if file exists without creating it

Tags:

java

file-io

If I do this:

File f = new File("c:\\text.txt");  if (f.exists()) {     System.out.println("File exists"); } else {     System.out.println("File not found!"); } 

Then the file gets created and always returns "File exists". Is it possible to check if a file exists without creating it?

EDIT:

I forgot to mention that it's in a for loop. So here's the real thing:

for (int i = 0; i < 10; i++) {     File file = new File("c:\\text" + i + ".txt");     System.out.println("New file created: " + file.getPath()); } 
like image 396
ThreaT Avatar asked Jul 02 '12 10:07

ThreaT


People also ask

Which method is used to determine if a file exists?

The File. Exists() method returns true if the file exists and false when the file doesn't exist or the caller does not have read access to the file. If you want to check the presence of a directory instead, use the Directory. Exists() method.

How do I check if a file exists in C++?

Use ifile. open(): ifile. open() is mainly used to check if a file exists in the specific directory or not.

How do you check if file does not exist in Java?

To test to see if a file or directory exists, use the “ exists() ” method of the Java java. io. File class. If the exists() method returns true then the file or directory does exist and otherwise does not exists.

How to check if a file exists in a file object?

No file will be create when you make a File object, it is only an interface. You can also add an extension property on File and/or Uri, to simplify usage further. Then just use uri.exists or file.exists to check.

What does it mean when a file is verified to not exist?

The file is verified to not exist. The file's status is unknown. This result can occur when the program does not have access to the file. If both exists and notExists return false, the existence of the file cannot be verified. Show activity on this post.

Is it possible to create a file on the file system?

Creating a File instance does not create a file on the file system, so the posted code will do what you require. Show activity on this post. The Files.exists method has noticeably poor performance in JDK 8, and can slow an application significantly when used to check files that don't actually exist.

How to check if a path exists or not?

The methods in the Path class are syntactic, meaning that they operate on the Path instance. But eventually you must access the file system to verify that a particular Path exists, or does not exist. You can do so with the exists (Path, LinkOption...) and the notExists (Path, LinkOption...) methods.


2 Answers

When you instantiate a File, you're not creating anything on disk but just building an object on which you can call some methods, like exists().

That's fine and cheap, don't try to avoid this instantiation.

The File instance has only two fields:

private String path; private transient int prefixLength; 

And here is the constructor :

public File(String pathname) {     if (pathname == null) {         throw new NullPointerException();     }     this.path = fs.normalize(pathname);     this.prefixLength = fs.prefixLength(this.path); } 

As you can see, the File instance is just an encapsulation of the path. Creating it in order to call exists() is the correct way to proceed. Don't try to optimize it away.

like image 156
Denys Séguret Avatar answered Sep 20 '22 13:09

Denys Séguret


Starting from Java 7 you can use java.nio.file.Files.exists:

Path p = Paths.get("C:\\Users\\first.last"); boolean exists = Files.exists(p); boolean notExists = Files.notExists(p);  if (exists) {     System.out.println("File exists!"); } else if (notExists) {     System.out.println("File doesn't exist!"); } else {     System.out.println("File's status is unknown!"); } 

In the Oracle tutorial you can find some details about this:

The methods in the Path class are syntactic, meaning that they operate on the Path instance. But eventually you must access the file system to verify that a particular Path exists, or does not exist. You can do so with the exists(Path, LinkOption...) and the notExists(Path, LinkOption...) methods. Note that !Files.exists(path) is not equivalent to Files.notExists(path). When you are testing a file's existence, three results are possible:

  • The file is verified to exist.
  • The file is verified to not exist.
  • The file's status is unknown. This result can occur when the program does not have access to the file.

If both exists and notExists return false, the existence of the file cannot be verified.

like image 31
ROMANIA_engineer Avatar answered Sep 18 '22 13:09

ROMANIA_engineer