import java.io.File;
public class GetCurrentDirectory {
public static void main(String[] args) {
// 获取当前工作目录
String currentDir = System.getProperty("user.dir");
System.out.println("当前工作目录: " + currentDir);
// 或者使用 File 类获取当前目录
File currentDirFile = new File(".");
try {
String absolutePath = currentDirFile.getCanonicalPath();
System.out.println("当前目录的绝对路径: " + absolutePath);
} catch (Exception e) {
e.printStackTrace();
}
}
}
new File(".") 创建了一个指向当前目录的 File 对象。通过调用 getCanonicalPath() 方法可以获取该目录的绝对路径。这两种方法都可以用来获取当前目录,选择其中一种即可。
上一篇:java定义类的关键字
下一篇:java实体转map
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站