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