Java 如何解析 new File() 中的相对路径? [英] How does Java resolve a relative path in new File()?

查看:247
本文介绍了Java 如何解析 new File() 中的相对路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解 Java 在创建 File 对象时解析相对路径的方式.

I am trying to understand the way Java resolves relative path in while creating a File object.

使用的操作系统:Windows

OS used: Windows

对于下面的代码片段,我收到一个 IOException 因为它找不到路径:

For the below snippet, I am getting an IOException as it cannot find the path:

@Test
public void testPathConversion() {
        File f = new File("test/test.txt");
        try {
            f.createNewFile();
            System.out.println(f.getPath());
            System.out.println(f.getAbsolutePath());    
            System.out.println(f.getCanonicalPath());
        } catch (Exception e) {
            e.printStackTrace();
        }
}

我的理解是,Java 将提供的路径视为绝对路径,并在路径不存在时返回错误.所以这是有道理的.

My understanding here is, Java treats the path provided as absolute and returns an error when the path does not exist. So it makes sense.

当我更新上面的代码以使用相对路径时:

When I update the above code to use relative path:

@Test
    public void testPathConversion() {
        File f = new File("test/../test.txt");
        try {
            f.createNewFile();
            System.out.println(f.getPath());
            System.out.println(f.getAbsolutePath());    
            System.out.println(f.getCanonicalPath());
        } catch (Exception e) {
            e.printStackTrace();
        }    
    }

它创建一个新文件并提供以下输出:

It creates a new file and provides the below output:

test..	est.txt
C:JavaForTesters	est..	est.txt
C:JavaForTesters	est.txt

在这种情况下,我的假设是,即使提供的路径不存在,因为路径包含/../",java 将其视为相对路径并在 user.xml 中创建文件.目录.所以这也是有道理的.

In this case, my assumption is, even though the path provided doesn't exist, because the path contains "/../", java treats this as a relative path and creates the file in the user.dir. So this also makes sense.

但是如果我更新如下相对路径:

But if I update the relative path as below:

   @Test
    public void testPathConversion() {
        File f = new File("test/../../test.txt");
        try {
            f.createNewFile();
            System.out.println(f.getPath());
            System.out.println(f.getAbsolutePath());
            System.out.println(f.getCanonicalPath());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

然后我得到 IOException: Access is denied.

Then I get IOException: Access is denied.

我的问题是:

  1. 为什么 "test/../test.txt" 被视为相对路径并在 "user.dir" 中创建文件,但是 "test/../../test.txt" 返回错误?它在哪里尝试为路径 "test/../../test.txt" 创建文件?
  2. 当没有找到指定的相对路径时,文件似乎是在user.dir中创建的.所以,在我看来,以下两个场景做了同样的事情:

  1. why "test/../test.txt" is treated as a relative path and creates the file in "user.dir" but"test/../../test.txt" returns an error? Where does it attempt to create the file for the path "test/../../test.txt"?
  2. When the specified relative path is not found, the file seems to be created in the user.dir. So, it appears to me that the below two scenarios does the same thing:

//scenario 1
File f = new File("test/../test.txt");
f.createNewFile();

//scenario 2
File f = new File("test.txt");
f.createNewFile();

那么在现实世界中是否存在使用场景 1 而不是场景 2 的情况?

So is there a real world case where one would use scenario 1 instead of scenario 2?

我想我在这里遗漏了一些明显的东西,或者从根本上误解了相对路径.我浏览了 File 的 Java 文档,但找不到对此的解释.Stack Overflow 上发布了很多关于相对路径的问题,但我查找的问题是针对特定场景的,并不完全是关于如何解决相对路径的.

I suppose I am missing something obvious here or have fundamentally misunderstood relative paths. I went through the Java docs for File and I am not able to find an explanation for this. There are quite a few questions posted in Stack Overflow regarding relative paths, but the ones I looked up were for specific scenarios and not exactly about how relative paths are resolved.

如果有人能解释一下这是如何工作的或指向一些相关链接,那就太好了?

It will be great if someone could please explain me how this works or point to some related links?

推荐答案

有一个工作目录的概念.
该目录由 .(点)表示.
在相对路径中,其他一切都与它相关.

There is a concept of a working directory.
This directory is represented by a . (dot).
In relative paths, everything else is relative to it.

简单地放置.(工作目录)是您运行程序的地方.
在某些情况下,可以更改工作目录,但通常这是
点代表什么.我认为在您的情况下这是 C:JavaForTesters.

Simply put the . (the working directory) is where you run your program.
In some cases the working directory can be changed but in general this is
what the dot represents. I think this is C:JavaForTesters in your case.

所以 test.. est.txt 的意思是:子目录 test
在我的工作目录中,然后上一级,然后是
文件 test.txt.这与 test.txt 基本相同.

So test.. est.txt means: the sub-directory test
in my working directory, then one level up, then the
file test.txt. This is basically the same as just test.txt.

欲了解更多详情,请点击此处.

For more details check here.

http://docs.oracle.com/javase/7/docs/api/java/io/File.html

http://docs.oracle.com/javase/tutorial/必不可少的/io/pathOps.html

这篇关于Java 如何解析 new File() 中的相对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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