import java.io.File;
public class CurrentPathExample {
public static void main(String[] args) {
// 获取当前工作目录路径
String currentPath = System.getProperty("user.dir");
System.out.println("当前工作目录: " + currentPath);
// 获取当前文件路径(相对路径)
File file = new File(".");
try {
String absolutePath = file.getCanonicalPath();
System.out.println("当前文件的绝对路径: " + absolutePath);
} catch (Exception e) {
e.printStackTrace();
}
}
}
System.getProperty("user.dir")
:获取当前工作目录的路径。这是 JVM 启动时所在的目录,通常是你运行程序的目录。new File(".")
:创建一个表示当前目录的 File
对象。通过调用 getCanonicalPath()
方法可以获取该文件的绝对路径。如果你需要更详细的解释或有其他问题,请告诉我!
上一篇:java集合类
下一篇:javaset转list集合
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站