java读取配置文件key/value

记录下来吧,以前都没有记录,总是一味的去baidu。。

从资源文件里读取值的类,文件后缀不一定要.Properties,只要里面内容如:url=www.cnsec.net 
可通过key(url)取得值-www.cnsec.net,简单、强大 


import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.util.Properties; 
/** 
* ReadProperties.java 
* Description: 读取操作属性配置文件 
* @author li.b 
* @version 2.0 
* Jun 26, 2008 
*/ 
public class ReadProperties { 

    /** 
     * Description: 获取属性配置文件 
     * @param path 资源文件路径 
     * @return    Properties Object 
     * @throws FileNotFoundException 
     * @throws IOException 
     */ 
    public static Properties getProperties(String path) throws FileNotFoundException, IOException{ 
        Properties props = null; 
        File file = new File(path); 
        if(file.exists() && file.isFile()){ 
            props = new Properties(); 
            props.load(new FileInputStream(file)); 
        }else{ 
            System.out.println(file.toString() + "不存在!"); 
        } 
        return props; 
    } 
    
    /** 
     * Description: 从属性文件获取值 
     * @param props Properties Object 
     * @param key 
     * @return 通过key匹配到的value 
     */ 
    public static String getValue(Properties props,String key,String encod){ 
        String result = ""; 
        String en = ""; 
        String localEN = System.getProperty("file.encoding"); 
        if(encod !=null && !encod.equals("") ){ 
            en = encod; 
        }else{ 
            en = localEN; 
        } 
        try { 
            key = new String(key.getBytes(en),"ISO-8859-1"); 
            result = props.getProperty(key); 
            if(!result.equals("")){ 
                    result = new String(result.getBytes("ISO-8859-1"),en);                
            } 
        } catch (Exception e) { 
        }finally{ 
            if(result == null)result = ""; 
            return result; 
        } 
    } 
    
    public static String getValue(Properties props,String key){ 
        return getValue(props, key, ""); 
    } 

    /**

     * 测试一下

     * @param args

     */

       public static void main(String[] args) {

   

    String path = System.getProperty("user.dir") ;

   

扫描二维码关注公众号,回复: 753334 查看本文章

    System.out.println(path);

   

    Properties props = null; 

try {

props = getProperties(path+"\\webservice接口.txt");

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

    String test = getValue(props, "监控告警");

    System.out.println(test);

}

猜你喜欢

转载自mengtaohj.iteye.com/blog/2248467