WEB开发网
开发学院WEB开发Jsp java获得配置文件数据 阅读

java获得配置文件数据

 2009-11-06 20:56:23 来源:WEB开发网   
核心提示:以下是需要获得配置文件的类,其中获得/config.PRoperties 文件的数据view plaincopy to clipboardprint?package com.chinacache.boss.queryservice.util; import java.io.IOException; import jav
以下是需要获得配置文件的类,其中获得 /config.PRoperties 文件的数据

view plaincopy to clipboardprint?
package com.chinacache.boss.queryservice.util;  
 
import java.io.IOException;  
import java.io.InputStream;  
import java.util.Properties;  
 
import org.apache.log4j.Logger;  
 
public final class ConfigConstant {  
  static Logger logger = Logger  
        .getLogger(ConfigConstant.class);  
 
    public static Properties p = new Properties();  
      
      
 
      public static void init() {  
      InputStream in = null;  
      try {  
        in = ConfigConstant.class 
            .getResourceAsStream("/config.properties");  
        if (in != null)  
          p.load(in);  
      } catch (IOException e) {  
        e.printStackTrace();  
        logger.error("load " + "/config.properties" + " into Contants error");  
      } finally {  
        if (in != null) {  
          try {  
            in.close();  
          } catch (IOException e) {  
          }  
        }  
      }  
    }  
 
    public static String getProperty(String key, String defaultValue) {  
      return p.getProperty(key, defaultValue);  
    }  
      
    public static String getProperty(String key) {  
      return p.getProperty(key);  
    }  
      
    public static Object setProperty(String key, String Value) {  
      return p.setProperty(key, Value);  
    }  
 
} 
package com.chinacache.boss.queryservice.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

public final class ConfigConstant {
static Logger logger = Logger
.getLogger(ConfigConstant.class);

public static Properties p = new Properties();



  public static void init() {
InputStream in = null;
try {
in = ConfigConstant.class
.getResourceAsStream("/config.properties");
if (in != null)
p.load(in);
} catch (IOException e) {
e.printStackTrace();
logger.error("load " + "/config.properties" + " into Contants error");
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
}
}

public static String getProperty(String key, String defaultValue) {
return p.getProperty(key, defaultValue);
}

public static String getProperty(String key) {
return p.getProperty(key);
}

public static Object setProperty(String key, String Value) {
return p.setProperty(key, Value);
}

}


此句是用来调用/config.properties 里的selectdays属性值:

String shouldLastDaysStr=ConfigConstant.getProperty("selectdays");



/config.properties 文件内容:
selectdays=33

basicbak=200808

Tags:java 获得 配置

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接