猿问

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

当我运行时:


public static void main(String[] args) {


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


    }

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


需要一些官方文档,其中此类目录的列表是列表,可以通过pom.xml和 viamain方法以编程方式访问。


哈士奇WWW
浏览 144回答 2
2回答

米脂

我们可以在您的项目中使用许多系统属性。有些列在官方文档中。休息一下,你可以得到一个小片段。System.getProperties().entrySet().forEach(e -> {        System.out.println(e.getKey() + " : " + e.getValue());    });

子衿沉夜

您可以使用 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);     } }或者您可以通过属性文件传入目录。
随时随地看视频慕课网APP

相关分类

Java
我要回答