码迷,mamicode.com
首页 > 其他好文 > 详细

读写CSV文件

时间:2015-04-25 22:53:18      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:csv


准备:需要引用javacsv.jar


CSV


   publicvoid readCsv()throws IOException {

       ArrayList<String[]> csvList = newArrayList<String[]>();

       String csvFilePath = sourcePath;

       CsvReader reader = newCsvReader(csvFilePath,‘,‘, Charset.forName("SJIS"));

       reader.readHeaders();

       while(reader.readRecord()) {

           csvList.add(reader.getValues());

       }

       reader.close();

       for (int row = 0;row < csvList.size(); row++) {

           String[] coldata=csvList.get(row);

           if(coldata!=null)

           {

               for(intcol=0;col<coldata.length;col++)

               {

                   System.out.println("ROW="+row+";COL="+col+";Data="+coldata[col]);

               }

           }

       }

   }

CSV


   publicvoidwriteCvs(ArrayList<String[]> dataList)throwsIOException {

       String csvFilePath = sumMetricsPath;

       CsvWriter wr = newCsvWriter(csvFilePath,‘,‘, Charset.forName("SJIS"));

       String[] header = { "Name","COL1","COL2","COL3","COL4","COL5" };


       wr.writeRecord(header);

       for(intindex=0;index<dataList.size();index++)

       {

           String[] data= dataList.get(index);

           wr.writeRecord(data);

       }

       wr.close();

   }




读写CSV文件

标签:csv

原文地址:http://blog.csdn.net/tiankefeng19850520/article/details/45273957

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!