1、开发配置文件
在开发中经常会用到一些配置文件,便于在运行期间可以通过修改配置文件实现灵活的切换功能。而读取配置文件有很多方法,这些方法使用又会不一样。
2、读取的方式
2.1、项目结构
config.json:
{ "env": "test", "port": "8088", "host": "127.0.0.1" }
2.2、读取项目内的文件
方法1:
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("config.json");
EnvConfig envConfig = mapper.readValue(inputStream, EnvConfig.class);
System.out.println("getClass().getClassLoader().getResourceAsStream() >>> " + mapper.writeValueAsString(envConfig));
方法2:
String path = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath() + "config.json";
System.out.println("path >>> " + path); File file = new File(path); EnvConfig envConfigFile = mapper.readValue(file, EnvConfig.class); System.out.println("this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath() >>> " + mapper.writeValueAsString(envConfigFile));
启动项目执行:
getClass().getClassLoader().getResourceAsStream() >>> {"env":"test","port":"8088","host":"127.0.0.1"}
path >>> /C:/demo-read-file/target/classes/config.json
this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath() >>> {"env":"test","port":"8088","host":"127.0.0.1"}
都能读取文件config.json。
2.3、打成jar包运行(读取jar内部配置)
打成jar包后,运行jar包。调用接口后,方法1读取成功(绿色),方法2读取失败(红色):
2.4、打成jar包运行(读取jar外部配置)
jar包和配置在同一个目录下:
config.json如下:
{ "env": "uat", "port": "8080", "host": "192.11.26.32" }
读取外部同级config.json:
FileInputStream inputStream = new FileInputStream(System.getProperty("user.dir") + File.separator + "config.json");
//也可以读取成功
//FileInputStream inputStream = new FileInputStream("config.json");
EnvConfig envConfigOuter = mapper.readValue(inputStream, EnvConfig.class);
System.out.println("outer FileInputStream() >>> " + mapper.writeValueAsString(envConfigOuter));
启动jar:(user.dir是当前 java -jar 执行的目录)
java -jar demo.read.file.jar
运行结果:
2.5、作为jar包被引用,读取其他项目配置
2.5.1 支持.properties或.xml
ObjectMapper mapper = new ObjectMapper(); static final String[] PROPERTIES_FILE_NAMES = new String[]{"config.properties", "config.xml"}; public void read() throws IOException { List<String> loadFiles = new ArrayList<>(); for (int i$ = 0; i$ < PROPERTIES_FILE_NAMES.length; ++i$) { String resourceName = PROPERTIES_FILE_NAMES[i$]; Enumeration urls = Demo.class.getClassLoader().getResources(resourceName); System.out.println(">>>>>>>>>>load file begin. resourceName:" + resourceName ); while (urls.hasMoreElements()) { Properties properties = new Properties(); URL url = (URL) urls.nextElement(); loadFiles.add(url.getFile()); InputStream input = null; try { URLConnection con = url.openConnection(); con.setUseCaches(false); input = con.getInputStream(); if (resourceName.endsWith(".xml")) { properties.loadFromXML(input); System.out.println(">>>>>>>>>>load xml finished. properties:" + mapper.writeValueAsString(properties)); } else { properties.load(input); System.out.println(">>>>>>>>>>load other finished. properties:" + mapper.writeValueAsString(properties)); } } finally { if (input != null) { input.close(); } } } } }
2.5.1.1 支持.properties
a、config.properties配置:
userDesc=u8fd9u662fu4e00u4e2au5ba2u6237u7aefu914du7f6eu7684u7528u6237u4fe1u606f
userSex=u7537
userName=client_user
b、打成jar包给另一个项目使用:
>>>>>>>>>>load file begin. resourceName:config.properties
>>>>>>>>>>load xml finished. properties:{"userDesc":"这是一个客户端配置的用户信息","userName":"client_user","userSex":"男"}
2.5.1.2 支持.xml
a、config.xml配置(如果是简单的key-value格式的,可以使用sun的 http://java.sun.com/dtd/properties.dtd 定义xml格式)
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="userDesc">这是一个客户端配置的用户信息</entry>
<entry key="userName">client_user</entry>
<entry key="userSex">男</entry>
</properties>
b、打成jar包给另一个项目使用:
>>>>>>>>>>load file begin. resourceName:config.xml
>>>>>>>>>>load xml finished. properties:{"userDesc":"这是一个客户端配置的用户信息","userName":"client_user","userSex":"男"}
2.5.1.3 支持.xml(复杂格式的配置)
1、自定义xml格式;
2、InputStream inputStream = Demo.class.getClassLoader().getResourceAsStream("config.xml");
3、从inputstream解析Document文档为对应的Object 。
2.5.2 支持json格式的配置
json配置:
{ "userDesc": "u8fd9u662fu4e00u4e2au5ba2u6237u7aefu914du7f6eu7684u7528u6237u4fe1u606f", "userName": "client_user", "userSex": "male", "address": { "province": "u4e0au6d77u5e02", "city": "u4e0au6d77u5e02", "area": "u5f90u6c47u533a", "detail": "u5929u94a5u6865u8defu0031u0036u0038u0038u5f04" } }
InputStream resourceAsStream = Demo.class.getClassLoader().getResourceAsStream(resourceName); UserInfoConfig userInfoConfig = mapper.readValue(resourceAsStream, UserInfoConfig.class);
执行结果: