java实现读取配置文件生成map对象

package com.tygy.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

public class ReadProperties {

	/**
	* 文件路径
	*/
	private static String filePath = "resources/clounm.properties";

	/**
	* 获取properties文件中的内容,并返回map
	* 
	* @return
	*/
	public static Map<String, String> getProperties() {
		Map<String, String> map = new HashMap<String, String>();
		InputStream in = null;
		Properties p = new Properties();;
		try {
			in = new BufferedInputStream(new FileInputStream(new File(filePath)));
			p.load(in);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Set<Entry<Object, Object>> entrySet = p.entrySet();
		for (Entry<Object, Object> entry : entrySet) {
			map.put((String) entry.getKey(), (String) entry.getValue());
		}
		return map;
	}

}

  配置文件

clounm.properties
## 需要执行的SQL语句
sql = select * from  table
## 字段对应关系 
clounm = id,name
## type
type = type
properties文件默认编码集为iso8859-1,设置为utf-8才可以使用中文注释

posted on 2019-07-18 15:31  天涯鬼域  阅读(4371)  评论(0编辑  收藏  举报

导航