我们可以在Java中访问哪些其他位置(例如“ user.dir”)?

时间:2019-02-07 06:24:05

标签: java documentation

当我跑步时:

public static void main(String[] args) {

        System.out.println(System.getProperty("user.dir"));

    }

它给出了我项目的根目录。同样,如果我指定${project.build.outputDirectory},它会给我/project_root/target的位置,但是当我尝试在主方法中访问它时,它会失败(给出null)。

需要一些官方文档,其中列出了此类目录,可以通过pom.xmlmain方法以编程方式进行访问。

2 个答案:

答案 0 :(得分:2)

我们可以在您的项目中使用很多系统属性。一些在官方文档中列出。休息一会儿就可以了。

System.getProperties().entrySet().forEach(e -> {
        System.out.println(e.getKey() + " : " + e.getValue());
    });

Official Doc

Blog

答案 1 :(得分:0)

您可以使用FileHandler

“%t”系统临时目录 “%h”“ user.home”系统属性的值

https://kodejava.org/how-do-i-get-operating-system-temporary-directory-folder/

 public class TempDirExample {
     public static void main(String[] args) {
         // This is the property name for accessing OS temporary directory
         // or folder.
         String property = "java.io.tmpdir";

         // Get the temporary directory and print it.
         String tempDir = System.getProperty(property);
         System.out.println("OS temporary directory is " + tempDir);
     }
 }

或者您可以通过属性文件传递目录。