代码之家  ›  专栏  ›  技术社区  ›  paul

我们还可以访问哪些其他位置,如Java中的“user.dir”?

  •  0
  • paul  · 技术社区  · 6 年前

    当我跑步时:

    public static void main(String[] args) {
    
            System.out.println(System.getProperty("user.dir"));
    
        }
    

    ${project.build.outputDirectory} 它给我的位置 /project_root/target 但当我尝试在main方法中访问相同的内容时,它失败了(给出 null

    需要一些官方文档,其中的目录列表是list,可以通过 pom.xml main 方法

    2 回复  |  直到 6 年前
        1
  •  2
  •   Ignatiamus    6 年前

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

    Official Doc

    Blog

        2
  •  0
  •   Dan Miller    6 年前

    您可以使用FileHandler

    “%t”系统临时目录

    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);
         }
     }
    

    也可以通过属性文件传入目录。