11/10/2009

Java load properties file

如何用Java去讀取外部resource檔,要怎麼做呢?
EIF.properties是個文件檔,放在class可以讀到的路徑下,在這裡是放在/bin。

PropertyLoader class
package com.iden.resource;

import java.util.Properties;

public class PropertyLoader {

 private Properties prop = null;
 private Boolean isSet;

 PropertyLoader() {
  prop = new Properties();
  try {
   prop.load(getClass().getResourceAsStream("/EIF.properties"));
  } catch (Exception e) {
   e.printStackTrace();
  }
  if (new String(
    prop.getProperty("PropertyLoaderTest.isSet") != null ? prop
      .getProperty("PropertyLoaderTest.isSet") : "").trim()
    .equalsIgnoreCase("1"))
   isSet = true;
  else
   isSet = false;
 }
 
 public static void main(String[] args){
  PropertyLoader loader = new PropertyLoader();
  System.out.println("PropertyLoaderTest.isSet=" + loader.isSet);
  
 }
}

EIF.properties
# 1 or 0  / 1:short data model: update data when start time, end time, shift time  0:every data update model
PropertyLoaderTest.isSet=1

沒有留言:

張貼留言