1. 程式人生 > >使用java程式碼讀取properties檔案內容工具類

使用java程式碼讀取properties檔案內容工具類

使用java程式碼讀取properties檔案內容,一般工作中框架使用配置讀取properties,這裡只是工具類

程式碼如下:

package demo.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class DAOwenjian {
	
	private static Properties pro=new Properties();
	static{/**靜態方法塊在類載入的時候使用反射的方法靜態獲取properties檔案的內容*/
		InputStream out=DAOwenjian.class
				.getClassLoader()
				.getResourceAsStream("proper.properties");
		try{
			pro.load(out);
		}catch(IOException e){
			e.printStackTrace();
		}
	}
	
	/**返回檔案內容*/
	public static String getKey(String key){
		return pro.getProperty(key);
	}
	
	public static void main(String[] args) {
		System.out.println(getKey("DAOComputer"));
	}
}