package com.xiaochen.javase.reflect.bean;
public class AboutPath {
public static void main(String[] args) {
/*
比较通用的一种路径,前提是这个文件必须在类路径下。
在src下的都是类路径下,src是类的根路径。
Thread.currentThread() 当前线程对象
getContextClassLoader() 是线程对象的方法,可以获取到当前线程的类加载器对象。
getResource() 获取资源,类加载器对象的方法,当前线程的类加载器默认从类的根路径下加载资源。
*/
String path = Thread.currentThread().getContextClassLoader()
.getResource("classinfo2.properties").getPath();
//采用以上代码可以拿到一个文件的绝对路径
System.out.println(path);
String path2 = Thread.currentThread().getContextClassLoader().
getResource("com/xiaochen/javase/reflect/bean/classinfo3.properties").getPath();
System.out.println(path2);
}
}
获取通用路径的方法
最新推荐文章于 2022-11-03 15:05:05 发布