读取配置文件

 

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;


public class Propertys {
    private static Logger log = LogManager.getLogger(Propertys.class);
    
    private static Properties props = new Properties();
    private static File file = new File("D:\\solrconf\\common.properties");
//    private static File file = new File("/root/ctr/conf/common.properties");
    private static InputStream  fis = null;
    private static FileOutputStream fos = null;    
    
    private static BufferedReader bf = null;
    /**
     * 单例
     */
    static{
        try {
            fis = new FileInputStream(file);
            //处理中文乱码
            bf = new BufferedReader(new InputStreamReader(fis, "utf-8"));
            props.load(bf);
            fis.close();
        } catch (IOException e) {
            log.info("===Propertys===load==异常==msg:"+e);
            fis = null;
        }
    }
    /**
     * 获取整个配置
     * @return
     */
    public static Properties getProps() {
        return props;
    }
    /**
     * 根据key获取value
     * @param key
     * @return
     */
    public static String getProval(String key){
        return props.getProperty(key);
    }
    /**
     * 修改key值,并保存到文件
     * @param key
     * @param value
     */
    public static void setProps(String key,String value){
        try {
            fos = new FileOutputStream(file);
            props.setProperty(key, value);
            props.store(fos, key+":"+value);
            fos.close();
        } catch (Exception e) {
            log.info("===Propertys===setProps==异常==msg:"+e);
            fos = null;
        }    
    }
}

 

posted @ 2014-08-11 15:03  大都督  阅读(244)  评论(0编辑  收藏  举报