/**
* 获得文件绝对路径的通用方式:
* 该文件必须在src下的类路径下,输入的路径是从src出发的相对路径。
* src是类的根路径。
*/
public class AboutPath {
public static void main(String[] args) {
/*
currentThread() 当前线程对象。
getContextClassLoader() 是线程对象的方法,获得当前线程的类加载器对象。
getResource("") 是类加载器对象的方法,默认从类的根路径下加载资源。
*/
String path = Thread.currentThread().getContextClassLoader()
.getResource("com/PathTest.properties").getPath();
// 以上方法可以获得文件的绝对路径。
System.out.println(path);
}
}
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交