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才可以使用中文注释


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM