准备:需要引用javacsv.jar
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]);
}
}
}
}
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