File.exists () returns false when the file exists

I ran into an error by which I cannot find any logic. I have this File object, which is created as follows:

File file = new File("utilities/data/someTextFile.txt"); 

Then I do file.exists() and it returns false (!?). If the file is not found, I register f.getAbsolutePath() in the file. When I look at the path, everything seems to be in order. I can copy the full path to the Run window on Windows, and the file opens normally.

The file exists at all times and is not deleted or changed while my application is running. It is located on the local machine.

This only happens in certain situations. I can reproduce the error at any time, but I am sure that the path to the file is not changed by the actions that I do to reproduce the error.

What can cause file.exists() to return false? Is it related to permissions or file locks, etc.?

+82
java file-io
May 28 '09 at 9:10
source share
13 answers

I see the following situation in Windows 7:

 file.exists() == false file.getAbsoluteFile().exists() == true 

This file is "var \ log", the absolute path refers to an existing file, which is located in the normal subdirectory (and not in the virtual storage). This can be seen from the IDE.

+36
Jul 07 2018-10-10T00:
source share

There seems to be a difference in the way the path is specified in Java.

For example, if the file path is specified as file:/C:/DEV/test.txt , then

 File f = new File(filename); f.exists(); 

will return false . The path may work in Explorer or in the browser, but it is a URL, not an absolute path to the file.

But, on the other hand, if the file path is specified as C:/DEV/test.txt , then

 File f = new File(filename); f.exists(); 

will return true because the path is not a url but an absolute path.

With the Spring Framework , which is what ResourceUtils.getFile(filename) does - where the name can be either a URL or an absolute file path.

+16
Feb 14 2018-12-12T00:
source share

If the process does not have permissions to determine if the file exists, it will return false. Perhaps you can open the file, but not say the usual methods if it exists.

+15
May 28 '09 at 9:20
source share

The above answers did not help me in this case. As stated above, I had:

 file.exists() => false file.getAbsoluteFile().exists => true 

The main reason for this was because the Windows 7 machine owner changed the registry for CMD so that it automatically runs a command to run in a specific directory for working with Python. This modification crippled Java 1.6 code, which apparently uses CMD for Windows for certain file operations such as exists() . Eliminating autorun from the registry solved the problem.

+11
Nov 29
source share

The new File command simply creates an instance of the file using the specified path name. In fact, it does not create a file on the hard drive.

If you say

 File file = new File ("path"); file.exists() 

This can return true only if an existing file with the same path existed. If you plan to scan the same file specified on the first line, you may need to use it that way.

 File file = new File ("path"); file.createNewFile(); file.exists(); 

Now this will return true.

+3
Oct 24 '13 at 4:48 on
source share

If you do not want to call getAbsoluteFile () every time you have to call a method, it is better to instantiate your file with an absolute path. This should do the trick:

 File file = new File("utilities/data/someTextFile.txt").getAbsoluteFile(); 

I suggest surrounding it with a try-catch block, BTW.

+3
Sep 22 '16 at 8:01
source share

Obviously, there are a number of possible reasons, and the previous answers document them well, but here is how I solved it in one specific case:

My student had this problem, and I nearly tore my hair, trying to figure it out. It turned out that the file does not exist, although it looked as if it were. The problem was that Windows 7 was set to "Hide file extensions for known file types." This means that if the file has the name "data.txt", its actual file name is "data.txt.txt".

Hope this helps others save themselves from hair.

+2
May 8 '15 at 2:20
source share

When ["Hide extensions for known file types." ] it is checked that windows open "t.txt.txt" when the type is "t.txt" in [explorer] / [run windows], but not programmatically.

+1
Jan 26 '11 at 10:16
source share

To summarize the problem, the problem occurs when converting the URL / URI to local paths.

 Example: URL url = file:/D:/code%20repo%20sample/sample.txt // To remove url reference String localPath = url.getPath(); > /D:/code%20repo%20sample/sample.txt // Decoding reserved characters in url from hexadecimal to character URLDecoder.decode(localPath, StandardCharsets.UTF_8.toString()); > /D:/code repo sample/sample.txt 

Hope this helps.

+1
Aug 14 '19 at 7:35
source share

If the situations in which it fails include starting it up as a different user and you are in Windows Vista Vista / Windows 7, this may be caused by VirtualStore, a mechanism in which Windows allows an unprivileged user to "write", I can’t. However, the changes are saved in "% USERPROFILE% \ AppData \ Local \ VirtualStore \", which are closed for each user account.

0
May 28 '09 at 9:50
source share

Good answers at all. I found this to be similar to a problem with Java that accesses the C: root directory on Windows. Any other directory must be accurate, but for some reason, a specific mention of C:\ or C: or C:/ may lead to an error. I solved this very similar problem by putting the mention in new File("C:"); and replacing it with a new File(System.getProperty("file.separator")); , or you should be able to hard code "\" instead of saying "c:" as your file directory, and this might work. Not elegant, but got a job for me in this project.

Hope this helps. There may not be a right solution, but at least it worked for me. I am on JRE 1.6, Win 7 . Hurrah!

Respectfully,

@ Carpenter1010

0
Mar 05 '13 at 8:23
source share

When nothing worked for me, I tried

 filePath = filePath.trim(); 

This will clear your string of any unwanted character.

0
May 01 '19 at 18:48
source share

I think you should use a backslash, for example:

File file = new file ("C: \\ User \\ utilities \\ data \\ someTextFile.txt"); (two backslashes, not a typo)

Must fix the problem :)

-one
Sep 21 '16 at 18:12
source share



All Articles