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

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

问题描述



我试图了解Java在创建文件对象时解析相对路径的方式。使用:Windows



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

  @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将提供的路径视为绝对值,当路径不存在时返回错误。所以这是有道理的。



当我更新上述代码以使用相对路径:

 code> @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();
}
}

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

  test \..\test.txt 
C:\JavaForTesters\test\ .. \test.txt
C:\JavaForTesters\test.txt

在这个情况,我的假设是,即使所提供的路径不存在,因为路径包含/../,所以java将其视为相对路径,并在 user.dir 。所以这也是有道理的。



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

 code> @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:访问被拒绝。 p>

我的问题是:


  1. 为什么test /。 ./test.txt被视为相对路径,并在user.dir中创建文件test /../../ test.txt返回错误?在哪里尝试创建文件路径test /../../ test.txt

  2. 当找不到指定的相对路径时,该文件似乎在 user.dir 中创建。所以,在我看来,以下两种情况也是一样的:

      //场景1 
    文件f = new File(test /../ test.txt);
    f.createNewFile();

    //场景2
    文件f = new File(test.txt);
    f.createNewFile();


所以有一个真实世界的情况,其中一个将使用情景1而不是情景2?



我想我在这里缺少一些明显的东西,或者从根本上误解了相对路径。我经历了Java文档的File,我无法找到解释。 Stack Overflow中有相关路径发布了很多问题,但我查找的是针对特定场景的,而不是完全解决相对路径的问题。



如果有人能够解释一下这个工作原理或指向某些相关链接,这将是很棒的。

解决方案

有一个工作目录的概念。

此目录由(点)。

在相对路径中,其他一切都是相对的。



简单把(工作目录)是你运行程序的地方。

在某些情况下,工作目录可以更改,但一般来说,这是点代表的是

。我认为这是 C:\JavaForTesters\ 在你的情况下。



所以 test\..\test.txt 表示:我的工作中的子目录 test

目录,然后一级,然后是

文件 test.txt 。这基本上与 test.txt 基本相同。



有关详情,请查看这里。 p>>>> http://docs.oracle.com/javase/7/docs/api/java/io/File.html



http://docs.oracle.com/javase/tutorial/essential/io/pathOps.html


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

OS used: Windows

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();
        }
}

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\..\test.txt
C:\JavaForTesters\test\..\test.txt
C:\JavaForTesters\test.txt

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();
        }
    }

Then I get IOException: Access is denied.

My questions are:

  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();
    

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

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.

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.

So test\..\test.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/essential/io/pathOps.html

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

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