1. 程式人生 > >java 更改properties 檔案 不需要重啟web服務讀取最新修改的值

java 更改properties 檔案 不需要重啟web服務讀取最新修改的值

 在專案開發中 有寫配置是寫在 properties 檔案中的 ,但有的時候需要更改值,但是發現需要重啟服務才能生效

/**
	 * 初始化配置檔案
	 */
	public void init(){
		
		try{
			InputStream is = Config.class.getResourceAsStream("/res/config.properties");
			properties = new Properties();
			properties.load(is);
			
		}catch (Exception e){
			throw new RuntimeException("Failed to get properties!");
		}
	}
 後來發現 這種載入方法會將config.properties檔案載入到記憶體中,在下次需要讀取時直接從記憶體中獲取檔案資訊,而不是再次讀取!
 所以需要 改變一下檔案的輸入流獲取方式 
String path = Config.class.getClassLoader().getResource("/res/config.properties").getPath();  
	InputStream is = new FileInputStream(path);  
	properties.load(is);