user1663380
user1663380

Reputation: 1013

Why does file.exists() fail?

I checked and even printed out the path and the path exists, yet it can't find the file. I tried the code on local and it worked, and I am not sure what the cause is though (in Java).

filepath = "C:/FolderA/test.html";
File f1 = new File(filepath)
if (!f1.exists()) {
    System.out.println("File does not exist");
}

Upvotes: 0

Views: 3216

Answers (4)

Joop Eggen
Joop Eggen

Reputation: 109613

A fun guy may have used a Cyrillic letter, e (?), in the file name (or directory name). So list the files in Java:

while (file.getPath().length() > 3 && !file.exists()) {
    System.out.println("No such file: " + file.getPath());
    file = file.getParentFile();
    if (file == null) {
        break;
    }
}

if (file != null) {
    String[] children = file.list();
    System.out.println("Siblings: " + Arrays.toString(children));
}

Or copy your own file up there.

Upvotes: 0

Stony
Stony

Reputation: 3624

If you tested it on your local machine with Windows, beware that the file name is NOT case-sensitive. If your server based on a Linux/Unix platform, the file name will be case-sensitive.

Please double check it.

Upvotes: 0

Stephen C
Stephen C

Reputation: 719576

I can think of three possible explanations for what you are seeing:

  1. It could be a broken short-cut. The javadoc for exists() says:

    Tests whether the file or directory denoted by this abstract pathname exists.

    If the pathname is the name of a broken symbolic link, then the file or directory denoted by the path does not exist. (And if you attempted to open it in Java, you would get an IOException.)

  2. The application does not have sufficient permission to see the file. For instance, if the permissions on FolderA were such that the program couldn't read it, exists() would have to return false for the entire path.

  3. Somehow you have managed to get a funky character into either the pathname of the file as stored in the file system, or the Java string literal in your program. There are certain Unicode characters in different western alphabets that look like Latin letters (a-z, A-Z) ... but aren't. These can be hard to spot, depending on the glyphs used to display the respective characters.


The first two theories can be tested by inspecting the files and directories involved.

The third one will entail examining your source code and a directory listing using some tool that can render the respective characters as hexadecimal.

It would also be worth seeing what happens if you try to open the pathname (for reading) from Java, and in (say) Notepad.

Upvotes: 2

Juned Ahsan
Juned Ahsan

Reputation: 68715

Your code is perfectly fine, you need to check your file. Make sure your file is actually test.html. Sometimes file may be named with an extension but is actually a different type of file. For example your file may be actually

test.html.html

but will show up as

test.html

Upvotes: 0

Related Questions