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

后端 未结 5 893
野趣味
野趣味 2021-01-20 22:55

Noticed this today.

Given that a file named \"existing\" exists in the PWD of a java process (windows).

new File(\"existing\").exists() => true
         


        
5条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-20 23:18

    The two argument constructor expects a parent directory name, so your second line looks for a file whose relative path is "/existing". On a linux type system, "/" is the root (as far as I know), so /existing is very unlikely to exist. On windows, I'm not sure what it interprets that as by default, but if I open up a command line and say cd /Desktop (working directory being my user folder) it says it can't find the path specified.

提交回复
热议问题