日期:2014-05-17  浏览次数:20831 次

Liunx,Windows兼容查询Properties文件key值

Liunx,Windows兼容查询Properties文件key值

要在 http请求中访问 这个静态方法。

技术建站经验指导

转载注明:http://blog.csdn.net/yjflinchong/article/details/8439118

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;

/**
 * 查询Properties 文件 key值
 * @author yijianfeng
 *
 */
public class PropertiesUtil{

    
    public static Map<String,Properties> properties_map = new HashMap<String,Properties>();
  
    
    /** 
     * 根据KEY,读取文件对应的值 
     * @param filePath 文件路径,即文件所在包的路径,例如:java/util/config.properties 
     * @param key 键 
     * @return key对应的值 
     */  
    public static String findData(String file, String key) {  
        try {  
        	if(properties_map.get(file)==null){
    			//获取绝对路径  
                String filePath = PropertiesUtil.class.getResource("/").toString()+file;
                filePath = URLDecoder.decode(filePath, "UTF-8");
                filePath = filePath.replace("file:", "");
                System.out.println("properties路径:"+filePath+" ========");
                Properties  properties = new Properties();
                InputStream in = new BufferedInputStream(new FileInputStream(filePath));  
                properties.load(in);  
                in.close();  
                properties_map.put(file, properties);
        	}
            String value = properties_map.get(file).getProperty(key);  
            return value;  
        } catch (Exception e) {  
            e.printStackTrace();  
            return null;  
        }
    }
    
}