java:new File(“”,“name”)!= new file(“name”)? (file constructor with empty string)

We have noticed this today

Since there is a file named "existing" in the PWD of java process (Windows)

new File("existing").exists() => true
new File("","existing").exists() => false
new File(".","existing").exists() => true

I expect that starting from Javadoc, the system related default directory will be "." All this was true, so it was unexpected

reflection?

thank you!

-roger-

Solution

The two parameter constructor requires the parent directory name, so the second line looks for the file with the relative path "/ existing" On Linux type systems, "/" is the root (as far as I know), so / existing is unlikely to exist On windows, I'm not sure what the default explanation is, but if I open a command line and say CD / desktop (the working directory is my user folder), it says it can't find the specified path

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>