Java:当设置“user.dir”时,File.exists()不一致。 [英] Java : File.exists() inconsistencies when setting "user.dir"

查看:106
本文介绍了Java:当设置“user.dir”时,File.exists()不一致。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



具有不同构造函数的Instanciating两个File对象导致 File.exists()方法。



下面的代码是一个抽象的,而不是实际的代码。我不相信这是一个File.separator问题。我首先要求得到早期的反应,以防万一我错过了一个很好理解的问题。现在看来重置 user.dir 系统属性是其中一个原因解决这个问题下面的代码现在是可重现和可用的。您可以复制/粘贴Java类并尝试它,它应该与我列出的结果保持一致。


$ b 设置: $ b

创建文件夹体系结构 C:\ toto\tmp\sub



在不包含 tmp / sub 子文件夹架构的文件夹中启动以下类。



代码:

  public class TestFileExists {

public static void main(String [] args){

System.setProperty(user.dir,C:\\toto\\);

File root = new File(tmp);

文件sub_a =新建文件(root,sub);

文件sub_b = new File(root.getAbsolutePath()+/ sub);

System.out.println(sub_a path?+ sub_a.getAbsolutePath());
System.out.println(sub_a exists?+ sub_a.exists());
System.out.println(sub_b path?+ sub_b.getAbsolutePath());
System.out.println(sub_b exists?+ sub_b.exists());
System.out.println(Path equals?+(sub_a.getAbsolutePath()。equals(sub_b.getAbsolutePath())));
System.out.println(Obj equals?+(sub_a.equals(sub_b)));





结果:

  sub_a路径? C:\ toto\tmp\sub 
sub_a存在吗?假
sub_b路径? C:\ toto\tmp\sub
sub_b存在?真
路径等于?真
Obj等于?假

我不明白行 sub_a存在吗?假的,结果是不一致的机器之间,也没有与根初始路径蚂蚁结果现在是一致的机器之间。

现在如果你通过从命令行调用java来重新执行这个类,从一个包含 tmp / sub 子文件夹体系结构(如果您从 D:\ 调用它,则具有 D:\ tmp \ sub

  sub_a路径? C:\ toto\tmp\sub 
sub_a存在吗?真
sub_b路径? C:\ toto\tmp\sub
sub_b存在?真
路径等于?真
Obj等于?假

sub_a 假阳性,因为它会检查是否存在另一个文件夹,而不是由 getAbsolutePath()描述的文件夹。



所以我强烈怀疑 File.exists()取决于实际的Java执行路径,并且该文件存在与绝对路径不一致,并且 exists()使用另一个不同于user.dir系统属性的路径来检查文件系统。



从<?p

解决方案

设置 user.dir 不受支持。它应该被认为是一个只读的属性。



例如评估
lockquote>

用户.dir在jvm启动时被初始化,应该被用作
informative / readonly系统属性,尝试通过命令行定制它
-Duser.dir = xyz将在执行时结束dependend /未指定的行为。

虽然这个文本是关于在命令行上设置的,但是通过 setProperty() 最有可能同样未定义。



如果没有设置用户.dir ,那么你发现一个真正的问题。


JRE 6, on Windows XP.

Instanciating two File objects with different constructors leads to inconsistent results in the File.exists() method.

Disclaimer : the code below is an abstract, not the actual code. I do not believe this is a File.separator issue at all. I first asked to get early reactions, in case I missed a well understood issue. It now seems that resetting the user.dir system property is one of the causes to this problem. The code below is now reproducible and usable as is. You can copy/paste the Java class and try it, it should behave consistently with what I have listed as results.

Setup:

Create the folder architecture C:\toto\tmp\sub.

Launch the following class from within any folder which does not contain a tmp/sub sub-folder architecture.

Code:

public class TestFileExists {

    public static void main(String[] args) {

        System.setProperty("user.dir", "C:\\toto\\");

        File root = new File("tmp");

        File sub_a = new File(root, "sub");

        File sub_b = new File(root.getAbsolutePath()+"/sub");

        System.out.println("sub_a path ? "+sub_a.getAbsolutePath());
        System.out.println("sub_a exists ? "+sub_a.exists());
        System.out.println("sub_b path ? "+sub_b.getAbsolutePath());
        System.out.println("sub_b exists ? "+sub_b.exists());
        System.out.println("Path equals ? "+ (sub_a.getAbsolutePath().equals(sub_b.getAbsolutePath())));
        System.out.println("Obj equals ? "+ (sub_a.equals(sub_b)));

    }

}

Result :

sub_a path ? C:\toto\tmp\sub
sub_a exists ? false
sub_b path ? C:\toto\tmp\sub
sub_b exists ? true
Path equals ? true
Obj equals ? false

I don't understand the line sub_a exists ? false, and the result is not consistent from machine to machine, nor with the root initial path ant the result is now consistent with from machine to machine.

Now if you reexecute the class by calling java from the command line, from a folder which does contain a tmp/sub sub-folder architecture (like if you call it from D:\, having D:\tmp\sub), you will get the expected :

sub_a path ? C:\toto\tmp\sub
sub_a exists ? true
sub_b path ? C:\toto\tmp\sub
sub_b exists ? true
Path equals ? true
Obj equals ? false

But the existence of sub_a is clearly a false positive, because it checks the existence of another folder than the one described by the getAbsolutePath().

So I strongly suspect that File.exists() depends on the actual Java execution path, and that file existence is not consistent with the absolute path, and exists() uses another path than the "user.dir" system property to check the file system.

Any idea where this problem could come from ?

解决方案

Setting user.dir is unsupported. It should be considered a read-only property.

For example the evaluation of Bug 4117557 in the Sun Bug Parade contains this text:

"user.dir", which is initialized during jvm startup, should be used as an informative/readonly system property, try to customize it via command line -Duser.dir=xyz will end up at implementation dependend/unspecified behavior.

While this text is about setting it on the command line, setting it via setProperty() is most likely equally undefined.

When you can reproduce the problem without setting user.dir manually, then you've found a genuine problem.

这篇关于Java:当设置“user.dir”时,File.exists()不一致。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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