码迷,mamicode.com
首页 > 其他好文 > 详细

Properties2Map

时间:2015-06-11 17:15:09      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Properties2Map {

	public static void main(String[] args) throws Exception {
		String path = "E:/usr/heliConfig/app/url.properties";
		readConfigForMap(path);

		try {
			Map<String, String> periodCodeMap = readConfigForMap(path);
			Set<String> set1 = periodCodeMap.keySet();
			for (String s : set1) {
				System.out.println(s + "," + periodCodeMap.get(s));
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 读取文件,生成 Map<String,String>,左边为key 右边为value
	 * 
	 * @param path
	 * @return
	 * @throws Exception
	 */
	public static Map<String, String> readConfigForMap(String path) {
		BufferedReader bf = null;
		try {
			InputStream in = new FileInputStream(new File(path));
			Reader reader = new InputStreamReader(in);
			bf = new BufferedReader(reader);
			String row = null;
			Map<String, String> sMap = new HashMap<String, String>();
			while (null != (row = bf.readLine())) {
				if (!row.equals("")) {
					if (row.startsWith("#")) {
						continue;
					}
					String key = row.substring(0, row.indexOf("="));
					String value = row.substring(row.indexOf("=") + 1, row.length());
					sMap.put(key, value);
				}
			}
			bf.close();
			return sMap;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}


Properties2Map

标签:

原文地址:http://my.oschina.net/ydsakyclguozi/blog/465604

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!