一、属性文件以下:java
10001=错误
二、文件读取主要代码spring
// getResource方式 URL resourceURI = getClass().getClassLoader().getResource(""); // getResourceAsStream方式 InputStream stream = getClass().getClassLoader().getResourceAsStream("errorCode.properties"); logger.info(">>>>>>>>>>>>>>>>>>>>>> errorCode stream " + stream); logger.info(">>>>>>>>>>>>>>>>>>>>>> resourceURI " + resourceURI); if (null == stream) { logger.warn("errorCode.properties file not find"); return; } InputStreamReader reader = new InputStreamReader(stream,"UTF-8"); properties.load(reader); Enumeration<?> keys = properties.propertyNames(); while(keys.hasMoreElements()) { String key = (String) keys.nextElement(); String value = properties.getProperty(key); logger.info("key :" + key + "- value:" + value); }
2.1 使用idea启动读取测试maven
2.2 使用java -jar xxx.jar启动测试ide
从运行结果来看,使用getResourceAsStream方式能够读到jar中的文件,而使用getResource读取为空,在jar文件中查找资源和在文件系统中查找资源的方式是不同的,尽可能使用Stream流的方式操做资源文件。spring-boot
使用spring-boot-maven-plugin插件打出的fat-jar是不能够做为其余应用的jar依赖的,由于没法向普通的依赖jar那样去读取jar中的一些文件,这个和spring-boot-maven-plugin打包机制有关,能够换成使用maven-jar-plugin插件替换进行成可运行的的jar测试