propertiesファイルコンテンツ操作ツールクラス

1502 ワード

 import java.io.IOException;    
 import java.io.InputStream;
 import java.util.Properties;
 import org.apache.log4j.Logger;
public class PropertiesConfigUtils {
 private static final String config_file_properties = "/config.properties";
 private static final Logger logger = Logger
   .getLogger(PropertiesConfigUtils.class);
 /**
  * 
  *  ,key value 
  * 
  * @date 2013-11-13  01:47:22
  * @param filePath
  * @param key
  * @return
  * @see [ 、 # 、 # ]
  */
 public static String getValue(String filePath, String key) {
  Properties props = new Properties();
  InputStream in = null;
  String value = "";
  try {
   in = PropertiesConfigUtils.class.getResourceAsStream(filePath);
   props.load(in);
   value = props.getProperty(key);
  } catch (Exception e) {
   logger.error(e);
  } finally {
   try {
    in.close();
   } catch (IOException e) {
    logger.error(e);
   }
  }
  return value;
 }
 /**
  *  key value
  * 
  * @date 2013-11-13  01:47:26
  * @param key
  * @return
  * @see [ 、 # 、 # ]
  */
 public static String getValue(String key) {
  Properties props = new Properties();
  InputStream in = null;
  String value = "";
  try {
   in = PropertiesConfigUtils.class
     .getResourceAsStream(config_file_properties);
   props.load(in);
   value = props.getProperty(key);
  } catch (Exception e) {
   logger.error(e);
  } finally {
   try {
    in.close();
   } catch (IOException e) {
    logger.error(e);
   }
  }
  return value;
 }
}