java中如何加载资源文件呢?
下文是笔者收集的加载资源文件的方法分享,如下所示:
实现思路:
使用getResource()方法即可加载资源文件
例:
package com.java265.other;
import java.io.InputStream;
public class test {
/*
* java265.com 获取资源文件的示例分享
*/
public static void main(String[] args) throws Exception {
test t = new test();
t.test();
}
public void test() throws Exception {
//方式1
InputStream is = this.getClass().getResource("D:\\test2.txt").openStream();
//方式2
InputStream is2 = Thread.currentThread()
.getContextClassLoader()
.getResource("D:\\test2.txt").openStream();
System.out.println(is);
System.out.println(is2);
}
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


