java: new File("", "name") != new File("name") ? (带有空字符串的文件构造函数)

发布于 2024-11-18 18:32:40 字数 464 浏览 0 评论 0 原文

今天注意到了这一点。

假设 java 进程(Windows)的 PWD 中存在名为“existing”的文件。

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

我预料到,来自 javadoc 系统相关默认目录为“.”而这些都是真的,所以这出人意料。

想法?

谢谢!

-罗杰-

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-

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

薆情海 2024-11-25 18:32:40

这就是正在发生的事情。但我同意,因为这很令人困惑

new File("", "test").getAbsolutePath() => /test
new File(".", "test").getAbsolutePath() => ${pwd}/test

,我不知道为什么会出现这种情况,因为我认为第一个也将是密码。

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.

帅气称霸 2024-11-25 18:32:40

我记得很多个月前就遇到过这么多,所以我对实际来源进行了一些挖掘。以下是来自 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. */

因此,不明显的行为似乎是由于遗留原因造成的。

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.

情域 2024-11-25 18:32:40

双参数构造函数需要父目录名称,因此第二行查找相对路径为“/existing”的文件。在linux类型系统上,“/”是根(据我所知),因此/existing不太可能存在。在 Windows 上,我不确定默认情况下它会解释什么,但是如果我打开命令行并说 cd /Desktop (工作目录是我的用户文件夹),它会说它不能找到指定的路径。

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.

心的憧憬 2024-11-25 18:32:40

来自 java.io.File

If parent is the empty string then the new File instance is created
by converting child into an abstract pathname and resolving the result
against a system-dependent default directory.

没有提及默认目录是什么。

From java.io.File:

If parent is the empty string then the new File instance is created
by converting child into an abstract pathname and resolving the result
against a system-dependent default directory.

There's no mention of what the default directory is.

柳若烟 2024-11-25 18:32:40

请记住,“”与 null 不同。因此

new File("", "existing").exists()

不假设 .目录。正如 @Dylan Halperin 所说,在 Linux 上使用“”定向到根/目录,正如我发现使用以下代码:

import java.io.*;
class FileTest {
    public static void main(String args[]) { 
        String nullStr = null;
        File f1 = new File(nullStr, "f1");
        File f2 = new File("", "tmp");
        System.out.println("f1.exists(): " + f1.exists());
        System.out.println("f2.exists(): " + f2.exists());
    }
}

输出:

f1.exists(): true
f2.exists(): true

是的,我在工作目录中创建了一个名为“f1”的文件。

Remember that "" is NOT the same as null. Thusly

new File("", "existing").exists()

does not assume the . directory. As @Dylan Halperin said, on Linux using "" directs to the root / directory, as I found using this code:

import java.io.*;
class FileTest {
    public static void main(String args[]) { 
        String nullStr = null;
        File f1 = new File(nullStr, "f1");
        File f2 = new File("", "tmp");
        System.out.println("f1.exists(): " + f1.exists());
        System.out.println("f2.exists(): " + f2.exists());
    }
}

Output:

f1.exists(): true
f2.exists(): true

Yes, I had created a file named "f1" in the working directory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文