加载资源文件一
来源:互联网 发布:闲鱼淘宝介入怎么评判 编辑:程序博客网 时间:2024/04/28 13:32
import java.net.URL;
public class TestMain {
public static void main(String[] args) {
/* function:得到根目录下的资源文件 */
// 方法一:使用本类的getResource()方法--本类对应当前目录
URL filePathUrl = TestMain.class.getResource("/bean.xml");
// 使用相对路径
URL f = TestMain.class.getResource("../../../bean.xml");
// 得到同级目录资源文件:TestMain.class.getResource("同级.xml");
// 在不同包内寻找资源(指定相对路径(需要文件分隔符"/"),其他均返回null。)
System.out.println("filePathUrl-->" + filePathUrl.getFile());
// 输出:filePathUrl-->/H:/study20160323/00/bin/bean.xml
// 方法二:使用本类的类加载器的getResource()方法--类加载器对应源目录(推荐)
URL f2 = TestMain.class.getClassLoader().getResource("bean.xml");
// 方法三:使用ClassLoader类的getSystemResource()方法
URL f3 = ClassLoader.getSystemResource("bean.xml");
// 方法四:4:使用Thread加载资源(推荐)
URL f4 = Thread.currentThread().getContextClassLoader()
.getResource("bean.xml");
}
public class TestMain {
public static void main(String[] args) {
/* function:得到根目录下的资源文件 */
// 方法一:使用本类的getResource()方法--本类对应当前目录
URL filePathUrl = TestMain.class.getResource("/bean.xml");
// 使用相对路径
URL f = TestMain.class.getResource("../../../bean.xml");
// 得到同级目录资源文件:TestMain.class.getResource("同级.xml");
// 在不同包内寻找资源(指定相对路径(需要文件分隔符"/"),其他均返回null。)
System.out.println("filePathUrl-->" + filePathUrl.getFile());
// 输出:filePathUrl-->/H:/study20160323/00/bin/bean.xml
// 方法二:使用本类的类加载器的getResource()方法--类加载器对应源目录(推荐)
URL f2 = TestMain.class.getClassLoader().getResource("bean.xml");
// 方法三:使用ClassLoader类的getSystemResource()方法
URL f3 = ClassLoader.getSystemResource("bean.xml");
// 方法四:4:使用Thread加载资源(推荐)
URL f4 = Thread.currentThread().getContextClassLoader()
.getResource("bean.xml");
}
0 0
- 加载资源文件一
- mybatis源码解析(一)加载资源文件
- 资源加载一
- 动态加载资源文件
- spring加载资源文件
- spring加载资源文件
- 加载属性资源文件
- 加载国际化资源文件
- java加载资源文件
- ServletContext加载资源文件
- 加载资源文件
- SPRING 加载资源文件
- PropertyPlaceholderConfigurer加载资源文件
- 资源文件加载
- pyqt 加载资源文件
- spring 加载资源文件
- ClassPathResource加载资源文件
- 加载资源文件
- 一行代码解决各种IE兼容问题,IE6,IE7,IE8,IE9,IE10
- POJ 2955 Brackets (基础)
- 数值相同的变量为何判断为不相等?
- 架构师的十项技能
- kafka源码解析之六SocketServer
- 加载资源文件一
- APUE之线程(三)
- Cocoapods教程(二)——开源
- Hdu 1596 find the safest road 变相最短路径 Dijstra算法
- [0] 数组和指针的前世今生
- jps 报process information unavailable解决办法
- 编写C++中的两个类 一个只能在栈中分配空间 一个只能在堆中分配
- js-全局变量与局部变量
- 读取BMP图片并显示到窗口.