欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java项目中key/value通过key获取value值

程序员文章站 2022-03-24 13:16:36
...
public class PropertyRead {

	public static String read(String name) {
		
		return read(name,"/String.properties");
	}
	
public static String read(String name,String path) {
	String result = "";
      InputStream in = null;  
      Properties props = new Properties();  
      in = new PropertyRead().getClass().getResourceAsStream(path);  

      try {  
          props.load(in);  
      } catch (IOException e) {  
          e.printStackTrace();  
      } finally {  
          if (in != null) {  
              try {  
                  in.close();  
              } catch (IOException ex) {  
              }  
          }  
      }  

      //输出属性文件中的信息   
      Set set = props.keySet();  
      Iterator it = set.iterator();  
      try {
      while (it.hasNext()) {  
          String key = (String) it.next();
          if (key.equals(name)) {
        	  result = new String(props.getProperty(key).getBytes("ISO-8859-1"),"utf-8");
		} 
       }  
      } catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
  
		return result;
	}
}

 

相关标签: key/value