gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:44:20 24 4
gpt4 key购买 nike

我是 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();

预期结果:

id|类型|值|键

4|excel|0|excel.sheet.no

5|excel|dd/MM/yyyy|excel.date.format

6|excel|0|excel.baserate.rownum

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

最佳答案

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

)

Use this method if you want to create a CSVFormat from scratch. All fields but the delimiter will be initialized with null/false.

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

CSVFormat.newFormat(delimiter.charAt(0)).withHeader(header)).
withRecordSeparator(System.getProperty("line.separator"));

Returns a new CSVFormat with the record separator of the format set to the specified character.

Note: This setting is only used during printing and does not affect parsing. Parsing currently only works for inputs with '\n', '\r' and "\r\n"

关于java - apache-commons-csv println 方法不会在输出中打印换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55606236/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com