java: new File(“”, “name”) != new File(

2020-04-10 04:10发布

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-

5条回答
smile是对你的礼貌
2楼-- · 2020-04-10 04:47

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.

查看更多
对你真心纯属浪费
3楼-- · 2020-04-10 04:48

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.

查看更多
三岁会撩人
4楼-- · 2020-04-10 04:58

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.

查看更多
何必那么认真
5楼-- · 2020-04-10 04:58

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.

查看更多
\"骚年 ilove
6楼-- · 2020-04-10 04:58

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.

查看更多
登录 后发表回答