Java学习-022-Properties 文件数据写入

Properties 配置文件写入主要通过 Properties.setProperty 和 Properties.store 两个方法,此文以一个简单的 properties 文件写入源码做示例。

小二上码。。。若有不足之处,敬请大神指正,不胜感激!

Properties 文件写入源码如下所示:

 1     /**
 2      * @function write data to text file by Properties
 3      * 
 4      * @author Aaron.ffp
 5      * @version V1.0.0: autoUISelenium main.java.aaron.java.tools FileUtils.java propertiesWrite, 2014-11-20 16:45:42 Exp $
 6      * 
 7      * @param filename : full path for config file
 8      * @param author   : creator
 9      * @param configs  : file contents
10      * @return boolean
11      */
12     public boolean propertiesWrite(String filename, String author, ArrayList<String[]> configs){
13         Properties properties = new Properties();
14         boolean success = false;
15         
16         /* 参数校验: 参数非法时, 抛出参数非法异常 */
17         if (filename == null || "".equals(filename) || author == null || "".equals(author) || configs.isEmpty()) {
18             throw new IllegalArgumentException();
19         }
20         
21         try {
22             FileOutputStream fos = new FileOutputStream(filename);
23             
24             for (String[] row : configs) {
25                 properties.setProperty(row[0].toString().trim(), row[1].toString().trim());
26             }
27             
28             properties.store(fos, "author:" + author);
29             fos.close();
30             
31             success = true;
32         } catch (IOException ioe) {
33             this.message = "文件 {" + filename + "} 写入失败!";
34             this.logger.error(this.message, ioe);
35         }
36         
37         return success;
38     }
Properties 文件写入源码

测试源码如下所示:

 1     /**
 2      * Test : Store data to properties file
 3      * 
 4      * @author Aaron.ffp
 5      * @version V1.0.0: autoUISelenium test.java.aaron.java.tools FileUtilsTest.java test_propertiesWrite, 2014-11-20 16:49:42 Exp $
 6      *
 7      */
 8     public void test_propertiesWrite(){
 9         this.message = "\n\n\nTEST:FileUtils.propertiesWrite(String filename, String author, ArrayList<String[]> configs)";
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 + "test_propertiesWrite.properties";
15         
16         ArrayList<String[]> configs = new ArrayList<String[]>();
17         
18         String author = "Aaron.ffp";
19         
20         for (int i = 0; i < 10; i++) {
21             String[] row = new String[2];
22             
23             row[0] = i + "";
24             row[1] = "The row number is " + (i + 1);
25             
26             configs.add(i, row);
27         }
28         
29         if (this.fu.propertiesWrite(filename, author, configs)) {
30             Assert.assertEquals(this.fu.propertiesGetValue(this.fu.propertiesRead(filename), "2"), "The row number is 3", "Test case failed.");
31         } else {
32             this.message = "Test case failed, please check the data file : " + filename; 
33             Assert.fail(this.message);
34         };
35     }
测试源码

测试源码中调用的 propertiesRead(String) 方法请参阅:获取 Properties 配置文件内容

测试源码中调用的 propertiesGetValue(Properties, key)方法请参阅:获取 Properties 配置文件中对应的配置项

上述方法中调用的 propertiesKeyIsExist(Properties, key)方法请参阅:判断 Properties 配置文件中是否存在对应的配置项

测试执行结果如下所示:

测试生成的  文件内容如下所示:

 

至此, Java学习-022-Properties 文件数据写入 顺利完结,希望此文能够给初学 Java 的您一份参考。

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

posted @ 2015-06-29 17:50  范丰平  Views(517)  Comments(0Edit  收藏  举报