|
| 1 | +package com.zhisheng.common.utils; |
| 2 | + |
| 3 | + |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStream; |
| 6 | +import java.net.URL; |
| 7 | +import java.util.*; |
| 8 | +import java.util.stream.Collectors; |
| 9 | + |
| 10 | +public class PropertiesUtil { |
| 11 | + /** |
| 12 | + * 默认属性集合(文件在Constants中配置) |
| 13 | + */ |
| 14 | + public static Properties defaultProp = null; |
| 15 | + /** |
| 16 | + * 所有读取过的属性集合 |
| 17 | + * 文件名 <-> 属性集合 |
| 18 | + */ |
| 19 | + public static Map<String, Properties> allProps = new HashMap<>(); |
| 20 | + |
| 21 | + // 初始化默认的属性集合 |
| 22 | + static { |
| 23 | + if (defaultProp == null && isExistProperties("application.properties")) { |
| 24 | + defaultProp = loadProperties("application.properties"); |
| 25 | + allProps.put("application.properties", defaultProp); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * 读取属性文件,并将读出来的属性集合添加到【allProps】当中 |
| 31 | + * 如果该属性文件之前已读取过,则直接从【allProps】获得 |
| 32 | + */ |
| 33 | + public static Properties getProperties(String fileName) { |
| 34 | + if (fileName == null || "".equals(fileName)) { |
| 35 | + return defaultProp; |
| 36 | + } else { |
| 37 | + Properties prop = allProps.get(fileName); |
| 38 | + if (prop == null) { |
| 39 | + prop = loadProperties(fileName); |
| 40 | + allProps.put(fileName, prop); |
| 41 | + } |
| 42 | + |
| 43 | + return prop; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * 解析属性文件,将文件中的所有属性都读取到【Properties】当中 |
| 49 | + */ |
| 50 | + protected static Properties loadProperties(String fileName) { |
| 51 | + Properties prop = new Properties(); |
| 52 | + InputStream ins = null; |
| 53 | + ins = PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName); |
| 54 | + if (ins == null) { |
| 55 | + System.err.println("Can not find the resource!"); |
| 56 | + } else { |
| 57 | + try { |
| 58 | + prop.load(ins); |
| 59 | + } catch (IOException e) { |
| 60 | + System.err.println("An error occurred when reading from the input stream, " + e.getMessage()); |
| 61 | + } catch (IllegalArgumentException e) { |
| 62 | + System.err.println("The input stream contains a malformed Unicode escape sequence, " + e.getMessage()); |
| 63 | + } |
| 64 | + } |
| 65 | + return prop; |
| 66 | + } |
| 67 | + |
| 68 | + public static Boolean isExistProperties(String filePath) { |
| 69 | + URL resource = PropertiesUtil.class.getClassLoader().getResource(filePath); |
| 70 | + return resource != null; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * 从指定的属性文件中获取某一属性值 |
| 75 | + * 如果属性文件不存在该属性则返回 null |
| 76 | + */ |
| 77 | + public static String getProperty(String fileName, String name) { |
| 78 | + return getProperties(fileName).getProperty(name); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * 从默认的属性文件中获取某一属性值 |
| 83 | + * 如果属性文件不存在该属性则返回 null |
| 84 | + */ |
| 85 | + public static String getProperty(String name) { |
| 86 | + return getProperties(null).getProperty(name); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @param properties |
| 91 | + * @return |
| 92 | + */ |
| 93 | + public static Map<String, String> getPropertyMap(Properties properties) { |
| 94 | + return properties.values() |
| 95 | + .stream() |
| 96 | + .collect(Collectors.toMap(k -> k.toString(), v -> v.toString())); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @return |
| 101 | + */ |
| 102 | + public static Map<String, String> getPropertyMap() { |
| 103 | + Map<String, String> allPropsMap = new HashMap<>(); |
| 104 | + allProps.values().forEach(props -> { |
| 105 | + final Map<String, String> mapProperties = getPropertyMap(props); |
| 106 | + allPropsMap.putAll(mapProperties); |
| 107 | + }); |
| 108 | + return allPropsMap; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * 获取所有的 key |
| 113 | + * @param fileName |
| 114 | + * @return |
| 115 | + * @throws IOException |
| 116 | + */ |
| 117 | + public static List<String> getkeys(String fileName) throws IOException { |
| 118 | + Properties prop = new Properties(); |
| 119 | + //获取输入流 |
| 120 | + InputStream in = PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName); |
| 121 | + //加载进去 |
| 122 | + prop.load(in); |
| 123 | + Set keyValue = prop.keySet(); |
| 124 | + List<String> list = new ArrayList<>(); |
| 125 | + for (Iterator it = keyValue.iterator(); it.hasNext();) |
| 126 | + { |
| 127 | + String key = (String) it.next(); |
| 128 | + list.add(key); |
| 129 | + } |
| 130 | + return list; |
| 131 | + } |
| 132 | + |
| 133 | + //test |
| 134 | + public static void main(String[] args) throws IOException { |
| 135 | + List<String> keys = getkeys("application.properties"); |
| 136 | + System.out.println(keys); |
| 137 | + for (String key:keys) { |
| 138 | + String value = PropertiesUtil.getProperty("application.properties", key); |
| 139 | + System.out.println(value); |
| 140 | + } |
| 141 | + loadProperties("application.properties").keySet().forEach(it -> System.out.println(it)); |
| 142 | + getkeys("application.properties").forEach(it -> System.out.println(it)); |
| 143 | + getProperties("application.properties").keySet().forEach(it-> System.out.println(it)); |
| 144 | + } |
| 145 | +} |
0 commit comments