首页 > 学院 > 开发设计 > 正文

java获得配置文件数据

2019-11-17 04:05:55
字体:
来源:转载
供稿:网友
以下是需要获得配置文件的类,其中获得  /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
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表