1. 程式人生 > >ResourceBundle與Properties讀取配置檔案

ResourceBundle與Properties讀取配置檔案

ResourceBundle與Properties的區別在於ResourceBundle通常是用於國際化的屬性配置檔案讀取,Properties則是一般的屬性配置檔案讀取。

ResourceBundle的單例使用如下

import java.util.ResourceBundle;

/**
 * 訪問配置檔案 - 單例
 * @author: cyq    
 * @since : 2015-09-30 10:17
 */

public class Configuration {
	
	private static ResourceBundle rb    			= null;
	private volatile static Configuration instance	= null;
	
	private Configuration(String configFile) {
		rb = ResourceBundle.getBundle(configFile);
	}
	
	public static Configuration getInstance(String configFile) {
		if(instance == null) {
			synchronized(Configuration.class) {
				if(instance == null) {
					instance = new Configuration(configFile);
				}
			}
		}
		return instance;
	}
	
	public String getValue(String key) {
		return (rb.getString(key));
	}
}

Properties的使用

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;

public class PropertiesMain {
	/**
	 * 根據Key讀取Value
	 *
	 * @param filePath
	 * @param key
	 * @return
	 */
	public static String getValueByKey(String filePath, String key) {
		Properties pps = new Properties();
		InputStream in = null;
		try {
			// in = new BufferedInputStream(new FileInputStream(filePath));
			in = PropertiesMain.class.getResourceAsStream(filePath);
			pps.load(in);
			String value = pps.getProperty(key);
			System.out.println(key + " = " + value);
			return value;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 讀取Properties的全部資訊
	 *
	 * @param filePath
	 * @throws IOException
	 */
	public static void getAllProperties(String filePath) throws IOException {
		Properties pps = new Properties();
		InputStream in = PropertiesMain.class.getResourceAsStream(filePath);
		pps.load(in);
		Enumeration en = pps.propertyNames(); // 得到配置檔案的名字

		while (en.hasMoreElements()) {
			String strKey = (String) en.nextElement();
			String strValue = pps.getProperty(strKey);
			System.out.println(strKey + "=" + strValue);
		}
	}

	/**
	 * 寫入Properties資訊
	 *
	 * @param filePath
	 * @param pKey
	 * @param pValue
	 * @throws IOException
	 */
	public static void writeProperties(String filePath, String pKey,
			String pValue) throws IOException {
		Properties pps = new Properties();

		InputStream in = new FileInputStream(filePath);
		// 從輸入流中讀取屬性列表(鍵和元素對)
		pps.load(in);
		OutputStream out = new FileOutputStream(filePath);
		Object setProperty = pps.setProperty(pKey, pValue);
		System.out.println(setProperty);
		// 將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流
		pps.store(out, "Update " + pKey + " name");
	}

	public static void main(String[] args) throws IOException {
		// 列印當前目錄
		// System.out.println(new File(".").getAbsolutePath());
		// 如果使用FileInputStream方式讀取檔案流 ./config/myconfig.properties
		// 如果使用getResourceAsStream方式讀取檔案流 /myconfig.properties
		// String value = getValueByKey("/myconfig.properties", "say.hello");
		// System.out.println(value);
		// getAllProperties("/myconfig.properties");
		// writeProperties("./config/myconfig.properties","long", "2112");
	}

}