最近搞一个简单的Demo项目的时候,须要读取Properties文件中的配置信息,不想一个个文件写代码读取,也不想引入其它庞大的框架来进行自动扫描读取,就本身写了一个简单的自动扫描class目录下全部properties文件中的配置信息的工具。java
JDK版本为1.8.git
PropertiesContext类是一个单例,持有全部读取出来的配置信息,经过该对象的方法拿到所须要的配置信息。并发
package org.cent.tools.properties_reader.context; import org.cent.tools.properties_reader.util.PropertiesReader; import java.util.Properties; /** * 配置信息上下文 * Created by cent on 2016/10/24. */ public enum PropertiesContext { /** * 枚举实现单例模式 */ INSTANCE; /** * 读取到的配置信息(内存) */ private Properties properties; /** * 初始化读取配置 */ { PropertiesReader reader=new PropertiesReader(); properties= reader.readAllProperties(); } /** * 根据key获取配置值 * @param key * @return */ public String getProperty(String key) { return (String) properties.get(key); } /** * 根据key获取配置值,若是配置值为null,则返回默认值 * @param key * @param defaultVal 默认值 * @return */ public String getPropertyOrDefault(String key, String defaultVal) { return properties.get(key) == null ? (String) properties.get(key) : defaultVal; } /** * 是否包含该key的配置 * @param key * @return */ public boolean containsKey(String key){ return properties.containsKey(key); } }
该类封装了配置文件读取的入口方法,其中:readAllProperties()方法为读取class目录下全部配置文件;readProperties(File path)为读取指定路径下的全部配置文件。app
package org.cent.tools.properties_reader.util; import java.io.File; import java.util.Properties; /** * Created by cent on 2016/10/24. */ public class PropertiesReader { /** * 读取全部目录配置文件 * @return */ public Properties readAllProperties(){ //获取class根目录 File root=new File(this.getClass().getClassLoader().getResource("").getFile()); return readProperties(root); } /** * 读取指定目录配置文件 * @param path 指定目录 * @return */ public Properties readProperties(File path){ return PropertiesFileUtil.recursiveFile(path, PropertiesFileUtil::checkAndReadProperties); } }
递归读取文件目录下的Properties文件,并存储到Properties对象中返回。框架
package org.cent.tools.properties_reader.util; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.Arrays; import java.util.Properties; import java.util.function.Function; /** * 配置文件工具类 * Created by cent on 2016/10/24. */ public abstract class PropertiesFileUtil { /** * 配置文件后缀名常量 */ private static final String PROPERTIES_SUFFIX=".properties"; /** * 递归各个文件夹读取配置文件,若是是文件执行回调函数 * @param root 根目录 * @param isFileThenDoFunction 若是是文件,则执行该回调函数 * @return */ public static Properties recursiveFile(File root, Function<File,Properties> isFileThenDoFunction){ Properties properties=new Properties(); if(root.exists()) { File[] files = root.listFiles(); //并发流读取properties文件 Arrays.asList(files).parallelStream().forEach(file -> { if(file.isFile()){ properties.putAll(isFileThenDoFunction.apply(file)); }else{ properties.putAll(recursiveFile(file,isFileThenDoFunction)); } }); } return properties; } /** * 判断是否配置文件,是则读取出来并返回 * @param file 需读取的文件 * @return */ public static Properties checkAndReadProperties(File file){ String suffix=file.getName().substring(file.getName().lastIndexOf("."),file.getName().length()); if(PROPERTIES_SUFFIX.equals(suffix)){ Properties properties=new Properties(); try { properties.load(new FileInputStream(file)); return properties; } catch (IOException e) { e.printStackTrace(); } } return new Properties(); } }
阿里Code源码地址:https://code.aliyun.com/cent/properties-reader.git函数