Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file.exists returns different results on Windows versus Linux

I know it's technically a bug to append whitespace around file names, but I'd just like to know why the following code returns true on Windows, but false on Linux:

public static void main(String[] args) {
    File file = new File("some_file_that_exists ");  // note the whitespace at the end
    System.out.println(file.exists());
}

I'm using Windows 7 64-bit with jdk1.6.0_31, and retried on Linux 2.6.18 with jdk1.6.0_06. Note that the file does not have a space at the end of its name.

like image 690
M A Avatar asked Dec 06 '25 03:12

M A


1 Answers

This should be because windows does not allow space at end, and when java calls OS, OS removes/does not consider space at end.

Whereas, linux allows space at end, so it does exact check and tries to find file with spaces at end of file name.

like image 192
codingenious Avatar answered Dec 08 '25 20:12

codingenious