java - File/FileInputStream 相对路径奇怪的行为

标签 java file

我将 java 属性 user.dir 设置为 /home/alex/projects/poltava/rpgu/workingdir。我在上面的文件夹中还有文件 q.txt

以下是代码片段及其返回值(在 = 之后):

System.getProperty("user.dir") = /home/alex/projects/poltava/rpgu/workingdir
new File(".").getAbsolutePath() = /home/alex/projects/poltava/rpgu/workingdir/.
new File(".").exists() = true
new File("q.txt").getAbsolutePath() = /home/alex/projects/poltava/rpgu/workingdir/q.txt
new File("q.txt").exists() = false
new File(new File("q.txt").getAbsolutePath()).exists() = true
new FileInputStream("q.txt") = threw FileNotFoundException

所以你可以看到文件确实存在于文件系统中。当我尝试用绝对路径获取它时,一切都很好。当我尝试使用相对路径获取它时,它失败了。

相对路径有什么问题?

已编辑:

演示问题的小型应用程序:

import java.io.File;

public class Test {
    public static void main(String[] args) {
        System.setProperty("user.dir", "/home/alex/projects/poltava/rpgu/workingdir");
        System.out.println(System.getProperty("user.dir"));
        System.out.println(new File("q.txt").exists());
        System.out.println(new File("q.txt").isFile());
        System.out.println(new File("q.txt").canRead());

        System.out.println(new File("q.txt").getAbsolutePath());
        System.out.println(new File(new File("q.txt").getAbsolutePath()).exists());
        System.out.println(new File(new File("q.txt").getAbsolutePath()).isFile());
        System.out.println(new File(new File("q.txt").getAbsolutePath()).canRead());

        try {
            new FileInputStream("q.txt");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

输出:

/home/alex/projects/poltava/rpgu/workingdir
false
false
false
/home/alex/projects/poltava/rpgu/workingdir/q.txt
true
true
true
java.io.FileNotFoundException: q.txt (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:146)
    at java.io.FileInputStream.<init>(FileInputStream.java:101)
    at Test.main(Test.java:24)

编辑 2:

我还尝试了另一个简单的例子:

File f = new File("q1.txt");
System.out.println(f.createNewFile());
System.out.println(f.getPath());
System.out.println(f.getAbsolutePath());

一个输出:

true
q1.txt
/home/alex/projects/poltava/rpgu/workingdir/q1.txt

结果文件是在我启动应用程序的目录中创建的。不在 user.dir 目录中。并且 getAbsolutePath() 返回不正确的文件路径。

最佳答案

我认为阅读 javadoc for File 会更好。 .

一些帮助您入门的解释:

对于您正在使用的构造函数:

public File(String pathname)

Creates a new File instance by converting the given pathname string into an abstract pathname. If the given string is the empty string, then the result is the empty abstract pathname.

所以基本上你得到的是一个文件实例,带有抽象路径名“q.txt”。

当您对此执行 getAbsolutePath() 时会发生什么。再次来自 javadoc:

public String getAbsolutePath()

Returns the absolute pathname string of this abstract pathname. If this abstract pathname is already absolute, then the pathname string is simply returned as if by the getPath() method. If this abstract pathname is the empty abstract pathname then the pathname string of the current user directory, which is named by the system property user.dir, is returned. Otherwise this pathname is resolved in a system-dependent way. On UNIX systems, a relative pathname is made absolute by resolving it against the current user directory.

On Microsoft Windows systems, a relative pathname is made absolute by resolving it against the current directory of the drive named by the pathname, if any; if not, it is resolved against the current user directory.

你看到发生了什么吗?特别是关于 user.dir?

更多提示:

现在创建另一个变量,例如

File newFile = new File(System.getProperty("user.dir"), "q.txt")

newFile 上尝试相同的操作。尝试在上一个和这个上使用 getParent()。您会看到不同之处。

我希望这有助于为您澄清几点:)

关于java - File/FileInputStream 相对路径奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18905914/

相关文章:

Java 字符串外部化键缺失

Java 使用任务本身的数据终止计时器任务

objective-c - iOS : I can't find my file?

asp.net - 从 Azure 网站清除临时 ASP.NET 文件

java - 使用 google texttospeech : Could not find TLS ALPN provider; no working netty-tcnative, Conscrypt 时出错,或 Jetty NPN/ALPN 可用

java - 在 Java main 方法中获取资源

java - 制作一个空心钻石,里面有一个单词

javascript - 我无法使用 Canvas 从 blob URL 中裁剪图像

python - 如何读取文本文件并将其逐字写入python中的另一个文件?

java - 如何检测文件(具有任何扩展名)是否存在于java中