Properties文件的讀取與寫入

Properties文件的讀取

import java.util.Properties;

Properties prop = new Properties();

String collectPlace = null;
try{
    prop.load(CooectPlaceController.class.getClassLoader().getResourceAsStream("field.properties"));//獲得服務器上WEB-INF下classes中properties文件,一定要是WEB-INF                                                                                                                                                                                           //下的路徑,否則在發佈項目後會有問題
    collectPlace = prop.getProperty("key").trim();
}catch(IOException e1){
    e1.printStackTrace;
}


Properties文件的寫入

Properties prop = new Properties();
try{
 prop.load(CollectPlaceController.class.getClassLoader().getResourceAsStream("field.properties"));
}catch(IOException e){
 e.printStackTrace;
}
prop.setProperty("key",value);
String filePath =CollectPlaceController.class.getClassLoader().getResource("/").getPath()+"field.properties";
OutputStream outputStream = null;
try{outputStream = new FileOutputStream(filePath);

prop.store(outputStream."set"); //存入文件
}catch(IOException){
e.printStackTrace();
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章