首页 > 其他 > 详细

读写CSV文件

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


准备:需要引用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文件

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!