0

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");
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1663380
  • 1,003
  • 4
  • 18
  • 21
  • 1
    are you sure the file exists ??, you do realize File constructor will not create a physical file. – PermGenError May 22 '13 at 13:37
  • 1
    Are the permissions set correctly so that the program executed using java has access to the file (if it really exists)? – Werzi2001 May 22 '13 at 13:38
  • would like some alternatives to file.exists(), sometimes it does not return the right value http://stackoverflow.com/questions/3833127/alternative-to-file-exists-in-java – user1663380 May 22 '13 at 13:40
  • i set permissions to Full control yet it is still returns false even the filepath exists – user1663380 May 22 '13 at 13:41
  • 4
    I tried your exact code and it works as expected. – Petr Janeček May 22 '13 at 13:42
  • Is "FolderA" the folder you are actually trying or just an example? Windows uses virtual translated directory names which do not really exist (for example for "Program Files"). Maybe this may cause errors using the virtual directory name in Java. – Werzi2001 May 22 '13 at 13:43
  • `I tried the code on local and it worked` ? so where it is not working? on other machine? same operating system or different? if different which one? are you sure there is file at same location on other system? – rahul maindargi May 22 '13 at 13:44
  • it does not work on the other machine , mine machine is windows7, and the other machine is Windows server 2008, yes i am sure i checked it 5 times for the filepath – user1663380 May 22 '13 at 13:46
  • when you type `dir c:\FolderA\test.html` from the command line can it find the file? – xagyg May 22 '13 at 13:46
  • My guess is that the file is some whacky windows shortcut or something – Bohemian May 22 '13 at 13:46
  • by the way, java version is jre 7 – user1663380 May 22 '13 at 13:48
  • yes it can find the file, and it's not a shortcut or anything, could be a java bug? – user1663380 May 22 '13 at 13:49
  • yes i verified it that it exists both places – user1663380 May 22 '13 at 13:53
  • i don't get it, is it possibly a security problem? is your win 2008 server configured, so that java can access the file (permissions)? – redc0w May 22 '13 at 13:58
  • not quite sure what you mean though, but i did implement some local security policies in win 2008 server – user1663380 May 22 '13 at 14:00
  • also saw some issues of file exists http://stackoverflow.com/questions/919918/file-exists-returns-false-when-file-exists – user1663380 May 23 '13 at 06:24

4 Answers4

2

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

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

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Stony
  • 3,541
  • 3
  • 17
  • 23
0

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138