import java.io.File;
public class CurrentDirectoryExample {
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();
}
}
}
File
对象(用 "."
表示当前目录),然后调用 getCanonicalPath()
方法获取当前目录的绝对路径。注意,这里可能会抛出异常,因此需要进行异常处理。这两种方法都可以用来获取当前目录,选择哪种方法取决于具体的需求和上下文。
上一篇:java ==
下一篇:java 获取当前时间的时间戳
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站