1. 程式人生 > >java專案中key/value通過key獲取value值

java專案中key/value通過key獲取value值

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;
	}
}