java:new File("“,”name“)!= new File(”name“)? (具有空字符串的文件构造函数) [英] java: new File("", "name") != new File("name") ? (file constructor with empty string)

查看:174
本文介绍了java:new File("“,”name“)!= new File(”name“)? (具有空字符串的文件构造函数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天注意到了。



假设在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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆