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