读写CSV文件

时间:2015-04-25 22:52:25   收藏:0   阅读:333


准备:需要引用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();

   }




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

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!