绝对路径被视为相对路径

Absolute path being treated as relative

我已经 运行 编写了一段代码,其中绝对路径被视为相对路径。 到目前为止,我无法在其他项目中重现这一点。

我在 Windows 10.

上使用带有 JDK 8u51 的 NetBeans 8.0.2

下面是一段代码:

public void setImagePath(String path) throws IOException {
    File file = new File(path);
    System.out.printf("path:     %s\n", path);
    System.out.printf("resolved: %s\n", file.getAbsolutePath());
    System.out.printf("test:     %s\n", new java.io.File("C:/users/rando/desktop/test.jpg").getAbsolutePath());
    img = ImageIO.read(file);
    reloadImage();
}

这是输出:

path:     ‪C:\users\rando\desktop\test.jpg
resolved: C:\Users\rando\Dropbox\Other\NetBeans Projects\Applications\test\‪C:\users\rando\desktop\test.jpg
test:     C:\users\rando\desktop\test.jpg

请注意,在 "resolved" 行的中途,绝对路径以 C:...

开始

任何人都可以阐明这一点吗?

我唯一的猜测是这是 Windows 10 期。

编辑:

所以,在某种程度上,这是一个 Windows 10 期。在 Windows 7 上,我总是通过转到其属性并从安全选项卡中复制对象名称来获取特定文件的完整路径。在 Windows 10 中,它显然现在有一个额外的控制字符。

您的路径字符串中似乎有一个 Unicode LRE 控制代码。最简单的修复是

path = path.replaceAll("\p{C}", "");

这将删除所有控制字符。