Java_getProperties以记之

 1   /**
 2    * 获取配置文件行值key value对
 3    * @param address
 4    *         配置文件路径
 5    * @return
 6    *         配置文件中所有键值对
 7    * @throws IOException
 8    */
 9   private List<String[]> getProperties(String address) throws IOException {  
10       //获取输入流
11       InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(address);
12       //读取字符串
13       BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
14       //接收键值对数组
15       List<String[]> liStr=new ArrayList<String[]>();
16       //接收输入流行字符串
17       String line = "";
18       try{
19           //读取每行数据,直到行为null
20            while ((line = in.readLine()) != null){
21                //分割字符串 留下key value
22                String[] lineArray=line.split("=");
23                //去除key值无用空格
24                lineArray[0]=lineArray[0].trim();
25                //去除value值无用空格
26                lineArray[1]=lineArray[1].trim();
27                //放进list
28                liStr.add(lineArray);
29            }
30       }catch (IOException ioE){  
31           //捕获IO异常
32           ioE.printStackTrace();  
33       }finally{  
34           //关闭输入流
35           inputStream.close();  
36       }  
37       //返回结果
38       return liStr;  
39   }

 

  以上,就是留着读Properties.为啥会有这样的需求捏?

  正常读的话,应该是由key获取value吧.像这样?↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

 1   /**
 2    * 获取配置文件单独key值内容
 3    * @param address
 4    *         配置文件路径
 5    * @param key
 6    *         需要获取内容的key值
 7    * @return
 8    *         所需key值得value
 9    * @throws IOException
10    */
11   private String getProperties(String address,String key) throws IOException {  
12       InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(address);  
13       Properties properties = new Properties();  
14       try{  
15           properties.load(inputStream);  
16       }catch (IOException ioE){  
17           ioE.printStackTrace();  
18       }finally{  
19           inputStream.close();  
20       }  
21       return properties.getProperty(key);  
22   }

  这样比较快吧.哇哈哈.

 

  ZhLingF   W  ←  It's also my name

posted @ 2015-07-21 10:20  ZhLingF  阅读(300)  评论(0编辑  收藏  举报