publicvoidreadCsv(Stringpath){ try{ //一般,UsesISO-8859-1astheCharset. CsvReadercr1=newCsvReader(path); //有中文的 CsvReadercr2=newCsvReader(newFileReader(newFile(path))); //需要指定读入编码的 CsvReadercr=newCsvReader(newInputStreamReader(newFileInputStream(newFile(path)),"UTF-8")); while(cr.readRecord()){ //当前行号,从0开始 System.out.println("currentrecord:"+cr.getCurrentRecord()); //本行内容 System.out.println("RawRecord:"+cr.getRawRecord()); //每一列的内容 System.out.println("getValues()"); for(Strings:cr.getValues()){ System.out.print("--"+s); } System.out.println(); } }catch(IOExceptione){ //TODO自动生成catch块 e.printStackTrace(); } }publicvoidwriteCsv(Stringpath,Interviewinterview){ try{ StringcsvFilePath=path;//测试地址 CsvWriterwr=newCsvWriter(csvFilePath,',',Charset.forName("utf-8")); String[]contents={"aaaaa","bbbbb","cccccc","ddddddddd","不知道中文会不会乱码呢"};//这个只是测试数据,具体要怎么样的形式保存待定 wr.writeRecord(contents); wr.close(); }catch(IOExceptione){ e.printStackTrace(); } }
1