Java学习-019-Properties 文件读取实例源代码

在这几天的学习过程中,有开发的朋友告知我,每个编程语言基本都有相应的配置文件支持类,像 Python 编程语言中支持的 ini 文件及其对应的配置文件读取类 ConfigParse,通过这个类,用户可以方便的修改 ini 配置文件。在 Java 中对应的配置文件为 properties 格式的文本文件,其对应的内容格式为 “键=值” ,文本注释信息可以用 “#” 注释。同时 Java 语言中对应的类为 Properties(java.util.Properties),即为读取 properties 文件的类,继承于 Hashtable (如下所示),用户使用这个类可以很方便的操作 properties 文件。

下面首先看一下其对应的结果主要方法,如下所示:

1、getProperty(String key),用指定的键在此属性列表中搜索属性。即通过参数 key 得到所对应的 value。
2、load(InputStream inStream),从输入流中读取属性列表(键和元素对)。通过对指定的文件进行装载来获取该文件中的所有键值对。
3、setProperty(String key, String value),调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置键-值对。
4、store(OutputStream out, String comments),以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。
5、clear(),清除所有装载的 键 - 值对。该方法在基类中提供。

下面通过针对 Properties 配置文件的读取进行实例代码演示,以加深对 Properties 类的理解。

小二上码。。。

以下为原始的 properties 文件内容:

 1 # oracle config
 2 db_type=oracle
 3 driver=oracle.jdbc.driver.OracleDriver
 4 host=127.0.0.1
 5 port=1521
 6 database=test
 7 user=aaron
 8 pass=ffp
 9 sql=select * from hr t where t.no = '1521'
10 desc=oracle\u6570\u636E\u5E93\u8FDE\u63A5\u914D\u7F6E
PropertiesRead.properties 文件内容

properties 配置文件方法的源码(返回 Properties):

 1     /**
 2      * @function 文件读取: properties 文件
 3      * @description PS:注意读取中文时的乱码处理
 4      * 
 5      * @author Aaron.ffp
 6      * @version V1.0.0: autoUISelenium main.java.aaron.java.tools FileUtils.java propertiesRead, 2014-11-20 16:29:09 Exp $
 7      * 
 8      * @param filename 文件路径
 9      * @return Properties
10      */
11     public Properties propertiesRead(String filename){
12         Properties properties = new Properties();
13         
14         /* 参数校验: 为null或空字符串时, 抛出参数非法异常 */
15         if (filename == null || "".equals(filename) || !assertFileType(filename, "PROPERTIES")) {
16             throw new IllegalArgumentException();
17         }
18         
19         try {
20             /* 获取文件数据流 */
21             InputStream is = new FileInputStream(filename);
22             /* 加载文件数据流 */
23             properties.load(is);
24             /* 关闭文件数据流 */
25             is.close();
26         } catch (IOException ioe) {
27             this.message = "文件 {" + filename + "} 读取失败!";
28             this.logger.error(this.message, ioe);
29         }
30         
31         return properties;
32     }
Java 类 Properties 读取 properties 配置文件方法源码

测试方法源码:

 1     /**
 2      * Test : properties read for txt file
 3      * 
 4      * @author Aaron.ffp
 5      * @version V1.0.0: autoUISelenium test.java.aaron.java.tools FileUtilsTest.java test_propertiesRead, 2014-11-20 16:35:09 Exp $
 6      *
 7      */
 8     public void test_propertiesRead(){
 9         this.message = "\n\n\nTEST:FileUtils.propertiesRead(String filename)";
10         this.logger.debug(this.message);
11         
12         this.fu = new FileUtils();
13         String filename = this.constantslist.PROJECTHOME + this.constantslist.FILESEPARATOR + 
14                           "testng-temp" + this.constantslist.FILESEPARATOR + "propertiesRead.properties";
15         
16         Properties prop = this.fu.propertiesRead(filename);
17         
18         // get keys from properties
19         Enumeration<?> enu = prop.propertyNames();
20         
21         // print-1
22         prop.list(System.out);
23         
24         System.out.println("\n\n");
25         
26         // print-2
27         String key = "";
28         while (enu.hasMoreElements()) {
29             key = (String)enu.nextElement();
30             System.out.println(key + " = " + prop.getProperty(key));
31         }
32     }
Java 类 Properties 读取 properties 配置类文件方法测试源码

程序运行结果如下所示:

PS:若需使用此文中的源码,需要将 properties 文件中的内容保存至本地,并将测试方法源码中的文件路径改为本地路径,同时对上述源码进行适当修改才可成功运行,请知悉!

 

至此, Java学习-019-Properties 文件读取实例源代码 顺利完结,希望此文能够给初学 Java 的您一份参考。

最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^

posted @ 2015-06-29 15:20  范丰平  Views(634)  Comments(0Edit  收藏  举报