2009/05/18

讀取Properties檔

java.util.Properties的功能很好用,不用特別花心思在文字檔的處理就可以把文字檔的設定給讀進來,
做為讀取或寫入系統的設定都非常方便,設定檔的格式只要滿足<propery name>=<property value>或
<propery name>:<property value>,Properties就可以幫我們讀出所需要的值。



//Read properties file
public void readProperties(){
InputStream propStream = null;
File file = new File(".");

try{
String path = file.getCanonicalPath();
String fileSeparator = System.getProperty("file.separator");
String name = path + fileSeparator + "fex.properties";
propStream = new FileInputStream(name);
fexProps.load(propStream);
propStream.close();
}catch(IOException ioe){
System.err.println("Can't open the file fex.properties");
}
}

//Show properties content in screen
Properties fexProps = new Properties(); //properties load from somewhere
fexProps.list(System.out);


沒有留言: