【发布时间】:2016-06-05 10:13:48
【问题描述】:
我正在尝试解析如下的 CSV 文件
String NEW_LINE_SEPARATOR = "\r\n";
CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator(NEW_LINE_SEPARATOR);
FileReader fr = new FileReader("201404051539.csv");
CSVParser csvParser = csvFileFormat.withHeader().parse(fr);
List<CSVRecord> recordsList = csvParser.getRecords();
现在文件得到了以 CRLF 字符结尾的正常行,但是对于几行,中间出现了额外的 LF 字符。 即
a,b,c,dCRLF --line1
e,fLF,g,h,iCRLF --line2
因此,解析操作会创建三个记录,而实际上它们只有两个。
有没有办法让出现在第二行中间的 LF 字符不被视为换行符并仅在解析时获取两条记录?
谢谢
【问题讨论】:
-
您可以尝试先将所有LF替换为空,例如:
String newLine = oldLine.replace("\n", "");,然后继续解析。 -
感谢@mnille,这是一个很好的解决方案。
标签: java csv apache-commons