将属性文件以key-value值形式输出的工具类
2016-05-03 21:47
441 查看
PropertyUtil .java
运行结果:
package com.lmb.util; import java.io.IOException; import java.util.ArrayList; import java.util.Enumeration; import java.util.HashMap; import java.util.Properties; import java.util.Set; public class PropertyUtil { private HashMap<String, String> map = null; private HashMap<String, String> reverseMap = null; private String fileDir = ""; /** * 功能描述:获取fileDir文件的内容,将其以key-value的形式解析到一个map中 * @param fileDir 文件名 */ public PropertyUtil(String fileDir) { this.fileDir = fileDir; map = new HashMap<String, String>(); try { String key = ""; String value = ""; Properties props = new Properties(); //得到fileDir文件的输入流并将其load到我们生成的Properties对象中 props.load(PropertyUtil.class.getClassLoader() .getResourceAsStream(fileDir)); //遍历这个Properties对象,将对象内容以key-value的形式解析到map中 Enumeration enumFile = props.keys(); for (int i = 0; i < props.size(); i++) { key = (String) enumFile.nextElement(); value = props.getProperty(key); map.put(key, value); } } catch (IOException e) { System.out.println("I/O error occurs when reading the file"); e.printStackTrace(); } } /** * 功能描述:获取fileDir文件的内容,将其以value-key的反转形式解析到一个map中 * @param fileDir 文件名 */ public void reverse() { reverseMap = new HashMap<String, String>(); try { String key = ""; String value = ""; Properties props = new Properties(); props.load(PropertyUtil.class.getClassLoader() .getResourceAsStream(fileDir)); Enumeration enumFile = props.keys(); for (int i = 0; i < props.size(); i++) { key = (String) enumFile.nextElement(); value = props.getProperty(key); reverseMap.put(value, key);//map.put(key, value) } } catch (IOException e) { System.out.println("I/O error occurs when reading the file"); e.printStackTrace(); } } /** * 功能描述:通过key获取到该key对应的value值 * @param key * @return value */ public String getValueByKey(String key) { return (String) map.get(key); } /** * 功能描述:获取map集合的所有key值 * @return */ public ArrayList getKeyList() { Set keySet = map.keySet(); Object[] keyArray = keySet.toArray(); ArrayList<String> keyList = new ArrayList<String>(); for (int i = 0; i < keyArray.length; i++) { String tempKey = (String) keyArray[i]; keyList.add(tempKey); } return keyList; } /** * 功能描述:获取反转map中key对应的value值(也就是正常情况下value对应的key) * @param key * @return */ public String getReverseValueByKey(String key) { if (reverseMap==null){ reverse(); return (String) reverseMap.get(key); }else{ return (String) reverseMap.get(key); } } /** * test * @param args */ public static void main(String[] args) { System.out.println("________test start!______"); PropertyUtil pu = new PropertyUtil("user.properties"); for (int i = 0; i < pu.getKeyList().size(); i++) { System.out.println(" " + pu.getKeyList().get(i) + "=" + pu.getValueByKey((String) pu.getKeyList().get(i))); } System.out.println(pu.getValueByKey("USERNAME").trim()); System.out.println("_________test end!_______"); } }
运行结果:
相关文章推荐
- StringBuilder 和StringBuffer 的区别
- 提取classes中properties的value(JAVA)
- UVA 11404 Palindromic Subsequence LCS
- 类型化和泛型化的 Table View Controller
- HashTable 和 HashMap的区别 StringBuffer 和Builder的区别
- fetchxml分页和QueryExpression分页
- 山东省第四届ACM省赛 A SDUT 2603 Rescue The Princess(简单数学题)
- java StringBuilder类的常用方法
- HDU 4740 The Donkey of Gui Zhou
- [leetcode] 347. Top K Frequent Elements
- [leetcode]Longest Increasing Subsequence
- LeetCode OJ 95. Unique Binary Search Trees II
- 关于UIView上添加一个手势,导致子视图为UIButton的无法响应
- requests中文文档
- 贝塞尔曲线 UIBezierPath
- iOS学习笔记-----UITextField与UITextView属性与方法
- 对指定UI控件进行指定截屏
- require.js的用法
- iOS xib创建UIScrollView不滑动问题解决
- UIImage两种初始化的区别