java 添加properties文件读写

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

/**
 * @author guoxiang
 *
 */
public class Config {

    private static Config instance = null;
    
    private Properties properties = null;
    
    private Config() {
        init();
    }
    
    public static Config getInstance() {
        
        if (instance == null) {
            instance = new Config();
        }
        return instance;
    }
    
    /**
     * 初始化配置文件
     */
    public void init(){        
        try{
            InputStream is = Config.class.getResourceAsStream("/config.properties");
            properties = new Properties();
            properties.load(is);            
        }catch (Exception e){
            throw new RuntimeException("Failed to get properties!");
        }
    }
    
    /**
     * 根据key值取得对应的value值
     * @param key
     * @return
     */
    public String getValue(String key) {
        return properties.getProperty(key);
    }

    /**
     * @return the properties
     */
    public Properties getProperties() {
        return properties;
    }
}

private static final String CONFIG_PATH = "config.path";
private static Properties config = null;
    
    
    static {
        try {
            config = com.tianditu.util.Config.getInstance().getProperties();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

strDirConfig = config.getProperty(CONFIG_PATH);




是网站的话放在WEB-INF\classes下, 不是网站放在bin下
#map url searchmap.url
= http://************* config.path = D:/ngis_config/

 

posted on 2012-12-14 15:48  雨渐渐  阅读(609)  评论(0编辑  收藏  举报

导航