[code]System.getProperty("user.dir")[/code]
[code]public class GetPath {
public static void main(String[] args) {
GetPath test = new GetPath();
System.out.println(test.getPath());
}
public String getPath(){
return this.getClass().getResource("/").getPath();
}
}[/code]
[code]File f = new File(".");
f.getCanonicalPath();[/code]
[code]public class GetPath {
public static void main(String[] args) {
GetPath test = new GetPath();
System.out.println(test.getPath());
}
public String getPath(){
return this.getClass().getResource("/").getPath();
}
}[/code]
[code]File f = new File(".");
f.getCanonicalPath();[/code]
本文介绍了几种在Java中获取当前程序运行路径的方法,包括使用System.getProperty(user.dir)获取当前工作目录,通过自定义类的方法返回类资源路径,以及利用File类获取当前目录的规范路径。

被折叠的 条评论
为什么被折叠?



