这是Java JDK中的错误吗? [英] Is this a bug in java jdk?

查看:73
本文介绍了这是Java JDK中的错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我得到一个代码为File file = new File("e:/");的java.lang.File类时,我当然得到了一个代表e:\目录的File类.

When I got a java.lang.File class with the code File file = new File("e:/");, of course I got a File class represented the e:\ directory.

但是,如果我得到了代码为File file = new File("e:");的File类,而我只是在驱动器E:中,那么我得到了一个File类,它表示当前目录.

But if I got a File class with code File file = new File("e:"); and I just in the drive E:, then I got a File class represented current directory.

假设我在目录E:\ dir \中,并且此目录中有一个名为Test.java的文件. 内容是:

Assume I'm in directory E:\dir\, And this directory have a file named Test.java. It's content is:

import java.io.File;
public class Test {
    public static void main(String[] args) {
        File file = new File("e:"); 
        File[] files = file.listFiles(); 
        for(File f: files){ 
            System.out.println(f + " " + f.exists()); 
        }
    }
}

打开cmd工具并导航到目录e:\ dir,在其中执行以下命令:

Open the cmd tool and navigate to the directory e:\dir, execute the following command in it:

E:\dir> javac Test.java
E:\dir> java Test

我得到了:

e:\Test.class false
e:\Test.java false

这是java jdk错误吗?

Is this a java jdk bug?

@JimGarrison的其他信息:

Additional information from @JimGarrison:

我运行了这段代码

public class Foo3
{
    public static void main(String[] args)  throws Exception
    {
        File f = new File("D:");
        System.out.println(f.getCanonicalPath());
        for (File x : f.listFiles())
            System.out.println(x + " " + x.getCanonicalPath() + " " + x.getAbsolutePath() + " " + x.exists() + " " + x.getAbsoluteFile().exists());
    }
}

在Eclipse(位于我的D:驱动器上)中,得到以下输出:

in Eclipse (which lives on my D: drive) and got the following output:

D:\dev\src\pdxep
D:\.classpath D:\dev\src\pdxep\.classpath D:\dev\src\pdxep\.classpath false true
D:\.project D:\dev\src\pdxep\.project D:\dev\src\pdxep\.project false true
D:\.settings D:\dev\src\pdxep\.settings D:\dev\src\pdxep\.settings false true
D:\gallery D:\dev\src\pdxep\gallery D:\dev\src\pdxep\gallery false true
D:\pom.xml D:\dev\src\pdxep\pom.xml D:\dev\src\pdxep\pom.xml false true
D:\src D:\dev\src\pdxep\src D:\dev\src\pdxep\src false true
D:\target D:\dev\src\pdxep\target D:\dev\src\pdxep\target false true

确认发生了什么有趣的事情.

Which confirms there's something funny going on.

Java 错误8130462 似乎与之相关,因为它与相对路径和绝对路径有关特别是在Windows中.

Java Bug 8130462 seems to be related as it has to do with relative vs absolute paths specifically in Windows.

推荐答案

关于获取用代码File file = new File("e:");表示当前工作目录的File的第一部分不是一个错误.这是Windows的驱动器相对路径".即,相对于指定驱动器中当前工作目录的路径. (是的,Windows每个驱动器都有不同的工作主管)

The first part about getting a File representing the current working directory with code File file = new File("e:"); is not a bug. It is a Windows "drive relative path". That is, a path relative to the current working directory in the specified drive. (Yes, Windows have a different working director per drive)

问题在于Java错误地在路径中的驱动器字母后添加了\,这使得该路径看起来像是绝对路径,并可能因此而错误地在file.exists()上返回了false.

The issue is that Java wrongly adds a \ after the drive-letter in the path which makes the path look like an absolute path, and wrongly returns false on file.exists() probably because of that.

但是,Java可以正确解析规范路径和绝对路径,并在x.getAbsoluteFile().exists()上正确返回true.正如示例代码中所注意到的,Java还可以正确地返回file.listFiles()CWD的内容.

However, Java correctly resolves the canonical-path and absolute-path and correctly returns true on x.getAbsoluteFile().exists(). Java also correctly returns the contents of the CWD in file.listFiles() as you noticed in your example code.

我在数据库中发现了一个旧错误,有关这个问题, JDK-5066567 或至少与此非常相似.它创建于2004年,2013年设置为进行中",当前的受让人"处于非活动状态",因此,我认为我们不会很快对此进行任何修复.

I found an old bug in the database which JDK-5066567 about this or at least very similar to this. It was created in 2004 and set to "In progress" 2013 and the current Assignee is "Inactive" so I don't think we will see any fix for this soon, if ever.

因此,要回答您的问题,我会说是的,这是一个错误.

So to answer you question, I would say yes, it is a bug.

但是,在java.nio.file.Path中似乎可以更好地处理它.因此,如果在您的用例中可以使用java.nio.file.*软件包,则可能是一种可接受的解决方法.

However, it seems like it is better handled in java.nio.file.Path. So if it is possible to use the java.nio.file.* package instead in your use case, it might be a acceptable workaround.

这篇关于这是Java JDK中的错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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