Changing the current working directory in Java?

前端 未结 14 1771
太阳男子
太阳男子 2020-11-22 05:32

How can I change the current working directory from within a Java program? Everything I\'ve been able to find about the issue claims that you simply can\'t do it, but I can\

14条回答
  •  我寻月下人不归
    2020-11-22 05:40

    You can use

    new File("relative/path").getAbsoluteFile()

    after

    System.setProperty("user.dir", "/some/directory")

    System.setProperty("user.dir", "C:/OtherProject");
    File file = new File("data/data.csv").getAbsoluteFile();
    System.out.println(file.getPath());
    

    Will print

    C:\OtherProject\data\data.csv
    

提交回复
热议问题