java - 相对路径的 File.createNewFile() 在更改 "user.dir"-Property 后使用错误的目录

标签 java path relative-path createfile system-properties

我想做的是在更改后在我的用户目录中创建一个具有相对路径的新文件。要更改我使用的用户目录 System.setProperty("user.dir", "/data"); ,然后我用 File f2 = new File("f2"); 创建了一个文件对象并使用 f2.createNewFile(); 在我的文件系统上创建了空文件。此后,我预计该文件会出现在/data/f2 中,这就是 f2.getAbsolutePath()告诉我。但是,令人困惑的是,该文件出现在“旧的、初始的”userDir 中。

这是我的测试:

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;

public class Main {

    private static FilenameFilter filter = new FilenameFilter() {

        public boolean accept(File dir, String name) {
            return (name.startsWith("f")) ? true : false;
        }
    };

    public static void main(String[] args) throws IOException {
        String userDirPropertyName = "user.dir";
        File initialUserDir = new File(System.getProperty(userDirPropertyName));

        System.out.println("files in " + initialUserDir.getAbsolutePath() + ":");
        for (File file : initialUserDir.listFiles(filter)) {
            System.out.println("  - " + file.getAbsoluteFile());
        }

        System.out.println("initial userDir = " + System.getProperty(userDirPropertyName));
        File f1 = new File("f1");
        f1.createNewFile();
        System.out.println("f1.getAbsolutePath() = " + f1.getAbsolutePath());
        System.out.println("getCanonicalPath() for . = " + new File(".").getCanonicalPath());

        System.setProperty(userDirPropertyName, "/data");

        System.out.println("changed userDir = " + System.getProperty(userDirPropertyName));
        File f2 = new File("f2");
        f2.createNewFile();
        System.out.println("f2.getAbsolutePath() = " + f2.getAbsolutePath());
        System.out.println("getCanonicalPath() for . = " + new File(".").getCanonicalPath());


        System.out.println("files in " + initialUserDir.getAbsolutePath() + ":");
        for (File file : initialUserDir.listFiles(filter)) {
            System.out.println("  - " + file.getAbsoluteFile());
        }
    }
}

这是我得到的输出:

files in /home/pps/NetBeansProjects/UserDirTest:  
initial userDir = /home/pps/NetBeansProjects/UserDirTest  
f1.getAbsolutePath() = /home/pps/NetBeansProjects/UserDirTest/f1  
getCanonicalPath() for . = /home/pps/NetBeansProjects/UserDirTest  
changed userDir = /data  
f2.getAbsolutePath() = /data/f2  
getCanonicalPath() for . = /data  
files in /home/pps/NetBeansProjects/UserDirTest:  
  - /home/pps/NetBeansProjects/UserDirTest/f1  
  - /home/pps/NetBeansProjects/UserDirTest/f2

虽然我更改了中间的 user.dir,但 f1 和 f2 出现在同一目录中?

最佳答案

我刚刚重现了您的场景。我认为您的错误是您尝试使用系统属性来更改当前工作目录。从系统属性中检索此目录的能力只是一种方便的方法。如果更改属性,目录本身不会更改。

解决方案是使用 File.mkdir()File.mkdirs() 创建您想要的目录,然后使用 new File(myDir, fileName) 其中 myDir 是您的新目录。

关于java - 相对路径的 File.createNewFile() 在更改 "user.dir"-Property 后使用错误的目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5030145/

相关文章:

java - Integer 和 IntWritable 类型的存在

java - 当应用程序部署为 JAR 时,为什么 ".."(两个句点)无法向上一级目录工作?

java - 在 Java 中查找从一个 URL 到另一个 URL 的相对路径

perl - perl 中的相对文件路径

c++ - 在 Xcode 中使用带有相对路径的 SDL_LoadBMP 有困难

java - 为 DAO 编写测试用例的最佳 TDD 方法是什么?

Eclipse 中的 Java Maven 项目创建失败

java - 为什么hashmap split方法需要在loHead.treeify(tab)之前判断if(hiHead!=Null)

vb.net - 更改保存我的设置的路径 - VB.NET 2008

path - 如何删除名称中有空格的文件夹(Golang)