InputStreamReader reader =new InputStreamReader(PropertyUtil.class.getClassLoader().getResourceAsStream(PATH),"UTF-8");


import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.OutputStream; 
import java.util.HashMap; 
import java.util.Iterator; 
import java.util.Map; 
import java.util.Properties; 
 
import org.apache.commons.logging.Log; 
import org.apache.commons.logging.LogFactory; 
   
   
public class PropertyUtil {   
    private  Log log = LogFactory.getLog(PropertyUtil.class);   
    private  String PATH = "captcha_sms.properties";   
    public static Map<String, String> listMap; 
    static{ 
    	PropertyUtil prop=new PropertyUtil(); 
    	listMap=prop.getAllValue(); 
    } 
   
    public  void setPATH(String pATH) {   
        PATH = pATH;   
    }   
   
   
    public  String getValue(String key) {   
        Properties props = new Properties();   
        try {   
			InputStreamReader reader =new InputStreamReader(PropertyUtil.class.getClassLoader().getResourceAsStream(PATH),"UTF-8");  
            props.load(reader);   
            String value = props.getProperty(key);   
            return value;   
        } catch (Exception e) {   
            e.printStackTrace();   
            log.error(e.getMessage());   
            return null;   
        }   
    }   
       
    public  Map<String, String> getValue( Map<String, String> map) {   
        Properties props = new Properties();   
        try {   
			InputStreamReader reader =new InputStreamReader(PropertyUtil.class.getClassLoader().getResourceAsStream(PATH),"UTF-8");   
            props.load(reader);   
            for (Map.Entry<String, String> entry : map.entrySet()) {   
                String key=entry.getKey();   
                String value = props.getProperty(key);   
                log.debug(key+":"+value); 
                map.put(key, value);   
            }   
            return map;   
        } catch (Exception e) {   
            e.printStackTrace();   
            log.error(e.getMessage());   
            return null;   
        }   
    }   
   
   
    public  void setValue(String key, String value) {   
        Properties prop = new Properties();   
        try {   
			InputStreamReader reader =new InputStreamReader(PropertyUtil.class.getClassLoader().getResourceAsStream(PATH),"UTF-8");  
            prop.load(reader);   
            OutputStream fos = new FileOutputStream(PropertyUtil.class.getClassLoader().getResource("").getPath() + PATH);   
            prop.setProperty(key, value);   
            prop.store(fos, "Update '" + key + "' value");   
        } catch (IOException e) {   
            log.error("Visit " + PATH + " for updating " +  key + " value error");   
        }   
    }   
     
	/** 
	 * 获取所有的 properties属性 
	 * @return 
	 */ 
	private  Map<String, String> getAllValue() { 
		Map<String, String> result=new HashMap<String, String>(); 
		Properties props = new Properties(); 
		try { 
			InputStreamReader reader =new InputStreamReader(PropertyUtil.class.getClassLoader().getResourceAsStream(PATH),"UTF-8"); 
			props.load(reader); 
	          Iterator<String> it=props.stringPropertyNames().iterator(); 
	          while(it.hasNext()){ 
	              String key=it.next(); 
	              System.out.println(key+":"+props.getProperty(key)); 
	              log.debug(key+":"+props.getProperty(key)); 
	              result.put(key, props.getProperty(key)); 
	          }			 
			return result; 
		} catch (Exception e) { 
			e.printStackTrace(); 
			log.error(e.getMessage()); 
			return result; 
		} 
	}	 
	 
	 
		 
	     
    public static void main(String[] args) {   
           
    	Map<String, String> map=PropertyUtil.listMap; 
    	 
    }   
} 



发布评论
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

Spring 定时器配置 基于注解 (使用 Spring4)知识解答
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。