Properties读取properties文件

在编写一个项目时,我们会时常修改一些配置变量来适应不同的运行环境,同时也让用户能够脱离程序去修改相关的变量设置。通常我们将这些变量定义在后缀名为properties的文件中,文件内容的格式为“键=值”,文本用“#”注释。
  而java提供了java.util.Properties来读取这些文件,具体Properties参见凭海临风的博客:http://www.cnblogs.com/bakari/p/3562244.html
  它提供了几个主要的方法:
  1. getProperty ( String key),用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。
  2. load ( InputStream inStream),从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 test.properties 文件)进行装载来获取该文件中的所有键 - 值对。以供 getProperty ( String key) 来搜索。
  3. setProperty ( String key, String value) ,调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。
  4. store ( OutputStream out, String comments),以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。
  5. clear (),清除所有装载的 键 - 值对。该方法在基类中提供。
  要获取定义在.properties中的值,首先要用Properties的方法load(InputStream inStream)来装载配置文件从而获取配置文件中的键值对,形成属性列表,其次用getProperty(String key)来获取属性列表中对应的值。
  此次主要有两种读取.properties文件的方法(在Java Project中):
  jdbc.properties:
[java]  view plain  copy
  1. driverClass = com.mysql.jdbc.Driver  
  2. jdbcURL=jdbc:mysql://127.0.0.1:3306/protest?useUnicode=true&characterEncoding=utf-8   
  3. user = root  
  4. password = root  

  1.使用FileInputStream("资源名称绝对路径")
    
[java]  view plain  copy
  1. public Properties propertiesFromFile() throws FileNotFoundException{  
  2.         properties = new Properties();  
  3.         InputStream in = null;  
  4.         try{  
  5.             in = new BufferedInputStream(new FileInputStream(propertiesPath));  
  6.             properties.load(in);  
  7.         }catch (FileNotFoundException e){  
  8.             e.printStackTrace();  
  9.         } catch (IOException e) {  
  10.             e.printStackTrace();  
  11.         }finally{  
  12.             try {  
  13.                 in.close();  
  14.             } catch (IOException e) {  
  15.                 e.printStackTrace();  
  16.             }  
  17.         }  
  18.         return properties;  
  19.     }  


  2.使用getClass.getResourceAsStream("资源名称") -推荐
[java]  view plain  copy
  1. public Properties propertiesFromClass(){  
  2.     properties = new Properties();  
  3.     InputStream in1 = this.getClass().getResourceAsStream(propertiesPath);  
  4.     InputStream is=PropertiesLoad.class.getResourceAsStream("/jdbc.properties");  
  5.     try {  
  6.         properties.load(in1);  
  7.     } catch (IOException e) {  
  8.         e.printStackTrace();  
  9.     }finally{  
  10.         try {  
  11.             in1.close();  
  12.         } catch (IOException e) {  
  13.             e.printStackTrace();  
  14.         }  
  15.     }  
  16.     return properties;  
  17. }  

     最后使用getProperty(key)获取properties中的值:
[java]  view plain  copy
  1. PropertiesLoad proLoad = new PropertiesLoad(propertiesPath);  
  2. String DriverClass = proLoad.propertiesFromClass().getProperty("driverClass");  

对于在程序中对properties文件的修改
[java]  view plain  copy
  1. PropertiesLoad proLoad = new PropertiesLoad(propertiesPath);  
  2. Properties pro = proLoad.propertiesFromClass();  
  3. OutputStream out = new FileOutputStream(DBconnect.class.getResource("/jdbc1.properties").toString().substring(6));  
  4. pro.setProperty("password", password+"1");  
  5. try {  
  6.      pro.store(out, "ceshiwenjian01");  
  7.      } catch (IOException e) {  
  8.     e.printStackTrace();  
  9.      }  
结果

jdbc1.properties:

[java]  view plain  copy
  1. #ceshiwenjian01  
  2. #Tue Aug 30 16:06:07 GMT+08:00 2016  
  3. driverClass=com.mysql.jdbc.Driver  
  4. user=root  
  5. password=root1  
  6. jdbcURL=jdbc\:mysql\://127.0.0.1\:3306/protest?useUnicode\=true&characterEncoding\=utf-8   

在上述的例子中,如果将Properties pro = proLoad.propertiesFromClass();换成Properties pro = new Properties();那么最后jdbc1.properties中的键值对只剩下password。所以所谓的setProperties(key,value)是将键值对写放入pro中,然后pro通过store将所有的pro里的键值对写入到文件中。上例中最后呈现的结果类似于改变键值对的值是因为pro装载了原来的键值对。

注:
在我们知道如何读写一个属性文件之后,我们仍然还有很多需要注意的问题,因为load和store方法都是按照ISO-8859-1的编码方式读写属性流文件的,而ILatin1 的字符和某些特殊字符,而对于非Latin1 的字符和某些特殊字符,则要使用与字符和字符串字面值所用的类似转义序列,以值和元素的形式来表示它们。所以当我们在处理中文时,不可以在直接修改属性文件时,将中文的值赋予给属性,而是要在JAVA程序中通过setProperty方法给属性赋予中文的值,因为这样store会将中文转换成 unicode码,在读取时系统会将读取到的unicode码按系统的编码打印出来,对于中文系统,通常是GBK码,这样中文才能够正常显示。

猜你喜欢

转载自blog.csdn.net/l18848956739/article/details/79518911