Noticed this today.
Given that a file named "existing" exists in the PWD of a java process (windows).
new File("existing").exists() => true
new File("", "existing").exists() => false
new File(".", "existing").exists() => true
I would have anticipated, from the javadoc that the system dependent default directory would be "." and these all be true, so this unexpected.
Thoughts?
Thanks!
-roger-
This is what's happening. But I agree because this is confusing
new File("", "test").getAbsolutePath() => /test
new File(".", "test").getAbsolutePath() => ${pwd}/test
I have no idea why this is the case because I had assumed it would also be pwd for the first one.
I remember encountering this many moons ago, so I did some digging in the actual source. Here is the relevant source documentation from File.java:
/* Note: The two-argument File constructors do not interpret an empty
parent abstract pathname as the current user directory. An empty parent
instead causes the child to be resolved against the system-dependent
directory defined by the FileSystem.getDefaultParent method. On Unix
this default is "/", while on Microsoft Windows it is "\\". This is required for
compatibility with the original behavior of this class. */
So, the non-obvious behavior appears to be due to legacy reasons.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With