最近遇到在java代码中读取自带文件,只知道可以采用getResourceAsStream,但对具体细节了解不甚详细,在网上查阅了相关资料,对各种情况做了一些汇总,mark一下。
以下是需要读取文件的各种场景:
- 文件和java文件的相对位置,各个properties文件
- 同时,在依赖的第三方包中还有文件
现在需要读取各个properties文件
测试代码
我们在TestPath.java中读取各个文件,主要采用
Class.getResourceAsStream(String path)
和
Class.getClassLoader().getResourceAsStream(path)
两种方式,测试代码如下:
/**
* Class.getResourceAsStream
*
* @param path
* 各个文件的路径
*/
public static void getResourceAsStream(String path) {
InputStream input = TestPath.class.getResourceAsStream(path);
showPath(input);
}
/**
* Class.ClassLoader.getResourceAsStream
*
* @param path
* 各个文件的路径
*/
public static void classLoadGetResourceAsStream(String path){
InputStream input = TestPath.class.getClassLoader().getResourceAsStream(path);
showPath(input);
}
/**
* print content in per file
* @param input
*/
public static void showPath(InputStream input) {
Properties prop = new Properties();
if (null == input) {
throw new RuntimeException("can not load properties: " + input);
}
try {
prop.load(input);
input.close();
for (Entry<Object, Object> entry : prop.entrySet()) {
System.out.println(entry.getKey().toString() + "="
+ entry.getValue().toString());
}
} catch (IOException e) {
throw new RuntimeException("can not read properties: " + input);
}
}
两种方式和文件路径的具体写法 :
- 在TestPath.java中采用Class.getResourceAsStream()
public static void testGetResourceAsStream() {
System.out.println("文件与当前class在同一目录");
getResourceAsStream("current_path.properties");
getResourceAsStream("/com/java/path/test/current_path.properties");
System.out.println("文件在当前class上级目录");
getResourceAsStream("../parent_path.properties");
getResourceAsStream("/com/java/path/parent_path.properties");
System.out.println("文件与当前class在不同的package");
getResourceAsStream("/com/java/pathanother/another_package.properties");
System.out.println("文件在class根目录");
getResourceAsStream("/root_path.properties");
System.out.println("文件位于的依赖第三方包");
getResourceAsStream("/META-INF/maven/commons-logging/commons-logging/pom.properties");
}
输出:
文件与当前class在同一目录
path=com.java.path.test
path=com.java.path.test
文件在当前class上级目录
path=com.java.path
path=com.java.path
文件与当前class在不同的package
path=com.java.pathanother
文件在class根目录
path=toot
文件位于的依赖第三方包
version=1.1.1
groupId=commons-logging
artifactId=commons-logging
- 采用Class.ClassLoader.getResourceAsStream
public static void testClassLoadGetResourceAsStream() {
System.out.println("文件与当前class在同一目录");
classLoadGetResourceAsStream("com/java/path/test/current_path.properties");
System.out.println("文件在当前class上级目录");
classLoadGetResourceAsStream("com/java/path/parent_path.properties");
System.out.println("文件与当前class在不同的package");
classLoadGetResourceAsStream("com/java/pathanother/another_package.properties");
System.out.println("文件在class根目录");
classLoadGetResourceAsStream("root_path.properties");
System.out.println("文件位于的依赖第三方包");
classLoadGetResourceAsStream("META-INF/maven/commons-logging/commons-logging/pom.properties");
}
输出:
文件与当前class在同一目录
path=com.java.path.test
文件在当前class上级目录
path=com.java.path
文件与当前class在不同的package
path=com.java.pathanother
文件在class根目录
path=toot
文件位于的依赖第三方包
version=1.1.1
groupId=commons-logging
artifactId=commons-logging