获取文本(txt) 键值对
Posted 南国的刘新
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取文本(txt) 键值对相关的知识,希望对你有一定的参考价值。
text:
key=value
key=value
key=value
key=value
类:
1 package com.holike.crm.mall.tools; 2 3 import java.io.File; 4 import java.util.concurrent.ConcurrentHashMap; 5 public class PropUtil { 6 /** 7 * PropKit. PropKit can load properties file from CLASSPATH or File object. 8 */ 9 10 11 private static Prop prop = null; 12 private static final ConcurrentHashMap<String, Prop> map = new ConcurrentHashMap<String, Prop>(); 13 14 private PropUtil() {} 15 16 /** 17 * Using the properties file. It will loading the properties file if not loading. 18 * @see #use(String, String) 19 */ 20 public static Prop use(String fileName) { 21 return use(fileName, "utf-8"); 22 } 23 24 /** 25 * Using the properties file. It will loading the properties file if not loading. 26 * <p> 27 * Example:<br> 28 * PropKit.use("config.txt", "UTF-8");<br> 29 * PropKit.use("other_config.txt", "UTF-8");<br><br> 30 * String userName = PropKit.get("userName");<br> 31 * String password = PropKit.get("password");<br><br> 32 * 33 * userName = PropKit.use("other_config.txt").get("userName");<br> 34 * password = PropKit.use("other_config.txt").get("password");<br><br> 35 * 36 * PropKit.use("com/jfinal/config_in_sub_directory_of_classpath.txt"); 37 * 38 * @param fileName the properties file‘s name in classpath or the sub directory of classpath 39 * @param encoding the encoding 40 */ 41 public static Prop use(String fileName, String encoding) { 42 Prop result = map.get(fileName); 43 if (result == null) { 44 result = new Prop(fileName, encoding); 45 map.put(fileName, result); 46 if (PropUtil.prop == null) 47 PropUtil.prop = result; 48 } 49 return result; 50 } 51 52 /** 53 * Using the properties file bye File object. It will loading the properties file if not loading. 54 * @see #use(File, String) 55 */ 56 public static Prop use(File file) { 57 return use(file, "utf-8"); 58 } 59 60 /** 61 * Using the properties file bye File object. It will loading the properties file if not loading. 62 * <p> 63 * Example:<br> 64 * PropKit.use(new File("/var/config/my_config.txt"), "UTF-8");<br> 65 * Strig userName = PropKit.use("my_config.txt").get("userName"); 66 * 67 * @param file the properties File object 68 * @param encoding the encoding 69 */ 70 public static Prop use(File file, String encoding) { 71 Prop result = map.get(file.getName()); 72 if (result == null) { 73 result = new Prop(file, encoding); 74 map.put(file.getName(), result); 75 if (PropUtil.prop == null) 76 PropUtil.prop = result; 77 } 78 return result; 79 } 80 81 public static Prop useless(String fileName) { 82 Prop previous = map.remove(fileName); 83 if (PropUtil.prop == previous) 84 PropUtil.prop = null; 85 return previous; 86 } 87 88 public static void clear() { 89 prop = null; 90 map.clear(); 91 } 92 93 public static Prop getProp() { 94 if (prop == null) 95 throw new IllegalStateException("Load propties file by invoking PropKit.use(String fileName) method first."); 96 return prop; 97 } 98 99 public static Prop getProp(String fileName) { 100 return map.get(fileName); 101 } 102 103 public static String get(String key) { 104 return getProp().get(key); 105 } 106 107 public static String get(String key, String defaultValue) { 108 return getProp().get(key, defaultValue); 109 } 110 111 public static Integer getInt(String key) { 112 return getProp().getInt(key); 113 } 114 115 public static Integer getInt(String key, Integer defaultValue) { 116 return getProp().getInt(key, defaultValue); 117 } 118 119 public static Long getLong(String key) { 120 return getProp().getLong(key); 121 } 122 123 public static Long getLong(String key, Long defaultValue) { 124 return getProp().getLong(key, defaultValue); 125 } 126 127 public static Boolean getBoolean(String key) { 128 return getProp().getBoolean(key); 129 } 130 131 public static Boolean getBoolean(String key, Boolean defaultValue) { 132 return getProp().getBoolean(key, defaultValue); 133 } 134 135 public static boolean containsKey(String key) { 136 return getProp().containsKey(key); 137 } 138 139 }
1 package com.holike.crm.mall.tools; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.io.InputStreamReader; 8 import java.util.Properties; 9 10 /** 11 * Prop. Prop can load properties file from CLASSPATH or File object. 12 */ 13 public class Prop { 14 private Properties properties = null; 15 16 /** 17 * Prop constructor. 18 * 19 * @see #Prop(String, String) 20 */ 21 public Prop(String fileName) { 22 this(fileName, "utf-8"); 23 } 24 25 /** 26 * Prop constructor 27 * <p> 28 * Example:<br> 29 * Prop prop = new Prop("my_config.txt", "UTF-8");<br> 30 * String userName = prop.get("userName");<br> 31 * <br> 32 * 33 * prop = new Prop("com/jfinal/file_in_sub_path_of_classpath.txt", 34 * "UTF-8");<br> 35 * String value = prop.get("key"); 36 * 37 * @param fileName 38 * the properties file‘s name in classpath or the sub directory 39 * of classpath 40 * @param encoding 41 * the encoding 42 */ 43 public Prop(String fileName, String encoding) { 44 InputStream inputStream = null; 45 try { 46 inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName); // properties.load(Prop.class.getResourceAsStream(fileName)); 47 if (inputStream == null) 48 throw new IllegalArgumentException("Properties file not found in classpath: " + fileName); 49 properties = new Properties(); 50 properties.load(new InputStreamReader(inputStream, encoding)); 51 } catch (IOException e) { 52 throw new RuntimeException("Error loading properties file.", e); 53 } finally { 54 if (inputStream != null) 55 try { 56 inputStream.close(); 57 } catch (IOException e) { 58 // LogKit.error(e.getMessage(), e); 59 e.printStackTrace(); 60 } 61 } 62 } 63 64 /** 65 * Prop constructor. 66 * 67 * @see #Prop(File, String) 68 */ 69 public Prop(File file) { 70 this(file, "utf-8"); 71 } 72 73 /** 74 * Prop constructor 75 * <p> 76 * Example:<br> 77 * Prop prop = new Prop(new File("/var/config/my_config.txt"), "UTF-8");<br> 78 * String userName = prop.get("userName"); 79 * 80 * @param file 81 * the properties File object 82 * @param encoding 83 * the encoding 84 */ 85 public Prop(File file, String encoding) { 86 if (file == null) 87 throw new IllegalArgumentException("File can not be null."); 88 if (file.isFile() == false) 89 throw new IllegalArgumentException("File not found : " + file.getName()); 90 91 InputStream inputStream = null; 92 try { 93 inputStream = new FileInputStream(file); 94 properties = new Properties(); 95 properties.load(new InputStreamReader(inputStream, encoding)); 96 } catch (IOException e) { 97 throw new RuntimeException("Error loading properties file.", e); 98 } finally { 99 if (inputStream != null) 100 try { 101 inputStream.close(); 102 } catch (IOException e) { 103 // LogKit.error(e.getMessage(), e); 104 e.printStackTrace(); 105 } 106 } 107 } 108 109 public String get(String key) { 110 return properties.getProperty(key); 111 } 112 113 public String get(String key, String defaultValue) { 114 return properties.getProperty(key, defaultValue); 115 } 116 117 public Integer getInt(String key) { 118 return getInt(key, null); 119 } 120 121 public Integer getInt(String key, Integer defaultValue) { 122 String value = properties.getProperty(key); 123 if (value != null) 124 return Integer.parseInt(value.trim()); 125 return defaultValue; 126 } 127 128 public Long getLong(String key) { 129 return getLong(key, null); 130 } 131 132 public Long getLong(String key, Long defaultValue) { 133 String value = properties.getProperty(key); 134 if (value != null) 135 return Long.parseLong(value.trim()); 136 return defaultValue; 137 } 138 139 public Boolean getBoolean(String key) { 140 return getBoolean(key, null); 141 } 142 143 public Boolean getBoolean(String key, Boolean defaultValue) { 144 String value = properties.getProperty(key); 145 if (value != null) { 146 value = value.toLowerCase().trim(); 147 if ("true".equals(value)) 148 return true; 149 else if ("false".equals(value)) 150 return false; 151 throw new RuntimeException("The value can not parse to Boolean : " + value); 152 } 153 return defaultValue; 154 } 155 156 public boolean containsKey(String key) { 157 return properties.containsKey(key); 158 } 159 160 public Properties getProperties() { 161 return properties; 162 } 163 }
调用:
Prop prop= PropUtil.use("file.txt");
prop.get("key");
以上是关于获取文本(txt) 键值对的主要内容,如果未能解决你的问题,请参考以下文章