【问题标题】:File.createNewFile() for relative path uses wrong directory after changing "user.dir"-Property更改“user.dir”-Property 后,相对路径的 File.createNewFile() 使用了错误的目录
【发布时间】:2011-06-29 03:29:07
【问题描述】:

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

这是我的测试:

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

f1 和 f2 出现在同一个目录中,尽管我在两者之间更改了 user.dir?

【问题讨论】:

标签: java path relative-path createfile system-properties


【解决方案1】:

在 Java 中无法更改当前目录。

当您更改系统属性时,属性会更改,但不会更改本机代码用于创建文件的实际工作目录。而 getAbsolutePath 则使用 user.dir 系统目录的当前值来构成绝对路径。

【讨论】:

    【解决方案2】:

    属性user.dir 的设置是not supported 并导致all kinds of strange effects

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

      • 谢谢你,我想我必须这样使用它。
      • @alexr 在某些时候正如这个测试指出的那样,改变 user.dir 是改变一些东西而不是其他东西(它不一致)stackoverflow.com/questions/45130661/…
      猜你喜欢
      • 2021-08-31
      • 2014-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-22
      • 1970-01-01
      • 2013-02-20
      相关资源
      最近更新 更多