java读取csv文件

1. 添加maven依赖


<dependency>
    <groupId>org.apache.commonsgroupId>
    <artifactId>commons-csvartifactId>
    <version>1.4version>
dependency>


2. 相关代码


public static List getcsvRecordListFromStr(String buff) throws IOException {
    ByteArrayInputStream tInputStringStream = new ByteArrayInputStream(buff.getBytes());
    Reader csvReader = new InputStreamReader(tInputStringStream, "UTF-8");
    CSVParser csvParser = new CSVParser(csvReader, CSVFormat.EXCEL.withHeader());
    List csvRecordList = csvParser.getRecords();
    return csvRecordList;
}




List records = CommonUtils.getcsvRecordListFromStr(buff);
records.remove(0);
for(CSVRecord record:records) {
    String settlementDay = record.get("结算日期\t").trim();
}

你可能感兴趣的:(java读取csv文件)