apache-commons-csv println 方法不会在输出中打印换行符

tha*_*b4u 4 java newline apache-commons-csv

我是 apache-commons-csv 1.6 的新手

我有一个基本要求,即打印 csv 文件,其中每条记录都换行。我正在尝试使用 CSVPrinter 的 println 方法。由于某些奇怪的原因,输出文件没有任何换行符。所有内容都打印在一行中。

我尝试在 Notepad++ 中打开输出并显示不可打印的字符。记录之间没有字符。任何帮助将不胜感激。谢谢。

CSVPrinter csvPrinter = null;

if(delimiter != null && delimiter.length() > 0) {
    csvPrinter = new CSVPrinter(new FileWriter(outputFile), CSVFormat.newFormat(delimiter.charAt(0)).withHeader(header));
}else {
    csvPrinter = new CSVPrinter(new FileWriter(outputFile), CSVFormat.DEFAULT.withHeader(header));
}

for(Map<String,String> record : inputList) {
    List<String> valueList = new ArrayList<String>();
    for(String key : record.keySet()) {
        valueList.add(record.get(key));
    }
    System.out.println(valueList);
    csvPrinter.printRecord(valueList);
    csvPrinter.println();
}
csvPrinter.close();
Run Code Online (Sandbox Code Playgroud)

预期结果:

id|类型|值|键

4|excel|0|excel.sheet.no

5|excel|日/月/年|excel.日期.格式

6|excel|0|excel.baserate.rownum

实际结果: id|type|value|key4|excel|0|excel.sheet.no5|excel|dd/MM/yyyy|excel.date.format6|excel|0|excel.baserate.rownum

use*_*900 5

如果您不想覆盖所有分隔符,请不要使用newFormat方法

如果您想从头开始创建 CSVFormat,请使用此方法。除分隔符外的所有字段都将使用 null/false 进行初始化。

如果要为每个记录添加新行分隔符,请添加withRecordSeparator

CSVFormat.newFormat(delimiter.charAt(0)).withHeader(header)).
 withRecordSeparator(System.getProperty("line.separator"));
Run Code Online (Sandbox Code Playgroud)

返回一个新的 CSVFormat,其格式的记录分隔符设置为指定字符。

注意:该设置仅在打印时使用,不影响解析。解析当前仅适用于带有 '\n'、'\r' 和 "\r\n" 的输入