java 项目结构:
application.properties
app.version=1.0
getClassLoader().getResource(“application.properties”)只支持web项目读取配置文件。
getClassLoader().getResourceAsStream(“application.properties”)支持web项目和jar包读取配置文件。
完整代码:
package com.test;
import java.io.*;
import java.net.URL;
import java.util.Properties;
public class JavaPath {
/**
* 读取配置文件,支持在web项目中和以jar包的形式读取。
*/
public static void bothJarAndWeb1() {
InputStream inputStream = JavaPath.class.getClassLoader().getResourceAsStream("application.properties");
commonMethod(inputStream);
}
/**
* 读取配置文件,支持在web项目中和以jar包的形式读取。
*/
public static void bothJarAndWeb2(){
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("application.properties");
commonMethod(inputStream);
}
/**
* 读取配置文件,只支持在web项目中读取。
*/
public static void onlyWeb(){
URL resource = JavaPath.class.getClassLoader().getResource("application.properties");
File file = new File(resource.getPath());
InputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
commonMethod(inputStream);
}
public static void commonMethod(InputStream inputStream){
Properties properties = new Properties();
try {
properties.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("app.version=" + properties.get("app.version"));
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
System.out.print("bothJarAndWeb1 output: ");
JavaPath.bothJarAndWeb1();
System.out.print("bothJarAndWeb2 output: ");
JavaPath.bothJarAndWeb2();
System.out.print("onlyWeb output: ");
JavaPath.onlyWeb();
}
}
直接在idea中运行输出结果:
bothJarAndWeb1 output: app.version=1.0
bothJarAndWeb2 output: app.version=1.0
onlyWeb output: app.version=1.0
打包成jar后运行输出结果:
java -jar javapath.jar
bothJarAndWeb1 output: app.version=1.0
bothJarAndWeb2 output: app.version=1.0
onlyWeb output: java.io.FileNotFoundException: file:/Users/xiaosan/Documents/work/javapath/out/artifacts/javapath_jar/javapath.jar!/application.properties (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at com.test.JavaPath.onlyWeb(JavaPath.java from InputFileObject:48)
at com.test.JavaPath.main(JavaPath.java from InputFileObject:61)
Exception in thread "main" java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:418)
at java.util.Properties.load0(Properties.java:337)
at java.util.Properties.load(Properties.java:325)
at com.test.JavaPath.commonMethod(JavaPath.java from InputFileObject:14)
at com.test.JavaPath.onlyWeb(JavaPath.java from InputFileObject:52)
at com.test.JavaPath.main(JavaPath.java from InputFileObject:61)
注意错误file:/Users/xiaosan/Documents/work/javapath/out/artifacts/javapath_jar/javapath.jar!/application.properties (No such file or directory)
中的javapath.jar!/application.properties
把jar文件当做了目录。
web项目与jar包的区别
- web项目放在servlet容器中运行,解压后是多个目录和文件,可以通过资源路径找到文件。
- jar包只是一个jar文件,不能通过资源路径寻找配置文件。