当我运行时:
public static void main(String[] args) {
System.out.println(System.getProperty("user.dir"));
}
它给出了我的项目的根位置。同样,如果我指定${project.build.outputDirectory}
它会给我/project_root/target
的位置,但是当我尝试在 main 方法中访问相同时,它会失败(给出null
(。
需要一些官方文档,其中列出了此类目录的列表,可以通过pom.xml
和main
方法以编程方式访问。
我们可以在您的项目中使用很多系统属性。有些列在官方文档中。休息你可以通过一小段得到。
System.getProperties().entrySet().forEach(e -> {
System.out.println(e.getKey() + " : " + e.getValue());
});
官方文档
博客
文件处理程序
"%t" 系统临时目录"%h"是"用户.主页"系统属性的值
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);
}
}
或者,您可以通过属性文件传入目录。