java:new File("“,”name“)!= new File(”name“)? (具有空字符串的文件构造函数) [英] java: new File("", "name") != new File("name") ? (file constructor with empty string)
问题描述
今天注意到了。
假设在java进程(windows)的PWD中存在一个名为existing的文件。
new File(existing)。exists()=> true
new File(,existing)。exists()=> false
new File(。,existing)。exists()=> true
我会从 javadoc 系统相关的默认目录将是。
谢谢!
-roger -
两个参数构造函数需要一个父目录名,行查找相对路径为/ existing的文件。在linux类型系统上,/是根(就我所知),所以/ existing是不太可能存在。在Windows上,我不知道它解释为默认,但如果我打开一个命令行,并说 cd / Desktop
(工作目录是我的用户文件夹)它说它找不到指定的路径。
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-
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.
这篇关于java:new File("“,”name“)!= new File(”name“)? (具有空字符串的文件构造函数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!