Java学习-016-CSV 文件读取实例源代码

上文(CSV文件写入)讲述了日常自动化测试过程中将测试数据写入 CSV 文件的源码,此文主要讲述如何从 CSV 文件获取测试过程中所需的参数化数据。敬请各位小主参阅,若有不足之处,敬请大神指正,不胜感激!

不多言,小二上码咯。。。

CSV文件读取源代码如下所示,敬请参阅!

 1     /**
 2      * @function Read File: CSV
 3      * 
 4      * @author Aaron.ffp
 5      * @version V1.0.0: autoUISelenium main.java.aaron.java.tools FileUtils.java csvRead, 2014-11-25 15:55:52 Exp $
 6      * 
 7      * @param filename  : CSV file
 8      * @param delimiter : content split char
 9      * @param encoding  : file encoding
10      * 
11      * @return ArrayList<String[]> file content
12      */
13     public ArrayList<String[]> csvRead(String filename, char delimiter, String encoding){
14         ArrayList<String[]> csvdata   = new ArrayList<String[]>(); // CSV 数据
15         CsvReader           csvreader = null;
16         
17         /* 参数校验: 为null或空字符串时, 抛出参数非法异常 */
18         if (filename == null || "".equals(filename) || !assertFileType(filename, "CSV")) {
19             throw new IllegalArgumentException();
20         }
21 
22         /* 分隔符默认处理, 若分割符号为空, 则默认 ‘,’ */
23         if ("".equals(delimiter)) {
24             delimiter = ',';
25         }
26 
27         try {
28             csvreader = new CsvReader(filename, delimiter, Charset.forName(encoding));
29             
30             /* 跳过表头, 若是需要表头则注释词句即可 */
31             // csvreader.readHeaders();
32             
33             while (csvreader.readRecord()) {
34                 csvdata.add(csvreader.getValues());
35             }
36         } catch (IOException ioe) {
37             this.message = "文件 {" + filename + "} 读取失败!";
38             this.logger.error(this.message, ioe);
39             
40             return null;
41         }
42 
43         return csvdata;
44     }
CSV文件读取源码

CSV文件读取测试源码如下所示,敬请参阅!

 1     /**
 2      * Test : read|write CSV file
 3      * 
 4      * @author Aaron.ffp
 5      * @version V1.0.0: autoUISelenium test.java.aaron.java.tools FileUtilsTest.java test_csvWrite_and_csvRead, 2014-11-25 16:28:20 Exp $
 6      *
 7      */
 8     @Test
 9     public void test_csvWrite_and_csvRead(){
10         this.message = "\n\n\nTEST:FileUtils.csvWrite(String filename, char delimiter, String encoding, ArrayList<String[]> csvdata)";
11         this.logger.debug(this.message);
12         
13         this.fu = new FileUtils();
14         
15         // you should change this to local file path
16         String filename = this.constantslist.PROJECTHOME + this.constantslist.FILESEPARATOR + 
17                           "testng-temp" + this.constantslist.FILESEPARATOR + "createfile.csv";
18         
19         ArrayList<String[]> csvdata = new ArrayList<String[]>();
20         String[] rows;
21         
22         // init csv data
23         for (int i = 0; i < 20; i++) {
24             rows = new String[10];
25             
26             for (int j = 0; j < rows.length; j++) {
27                 rows[j] = i + " = " + j;
28             }
29             
30             csvdata.add(rows);
31         }
32         
33         // init csv file data
34         boolean w = this.fu.csvWrite(filename, ',', "UTF-8", csvdata);
35         
36         // get actual data and compare data
37         boolean r = this.fu.csvRead(filename, ',', "UTF-8").get(2)[2].toString().equals("2 = 2");
38         
39         Assert.assertEquals(w, r, "Test case failed.");
40     }
CSV文件读取测试源码

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

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

posted @ 2015-06-26 15:09  范丰平  Views(437)  Comments(0Edit  收藏  举报