Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java apache commons csv println方法不';在输出中不打印换行符_Java_Newline_Apache Commons Csv - Fatal编程技术网

Java apache commons csv println方法不';在输出中不打印换行符

Java apache commons csv println方法不';在输出中不打印换行符,java,newline,apache-commons-csv,Java,Newline,Apache Commons Csv,我是apache commons csv 1.6的新手 我有一个基本要求,打印csv文件的每一条记录在新行。我正在尝试使用CSVPrinter的println方法。出于某种奇怪的原因,输出文件没有任何换行符。所有的东西都用一行打印出来 我尝试用Notepad++打开输出并显示不可打印的字符。记录之间没有字符。任何帮助都将不胜感激。谢谢 CSVPrinter csvPrinter = null; if(delimiter != null && delimiter.length()

我是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();
CSVPrinter CSVPrinter=null;
if(delimiter!=null&&delimiter.length()>0){
csvPrinter=new-csvPrinter(新的文件写入程序(输出文件),CSVFormat.newFormat(delimiter.charAt(0)).withHeader(header));
}否则{
csvPrinter=新的csvPrinter(新的FileWriter(outputFile),CSVFormat.DEFAULT.withHeader(header));
}
用于(映射记录:inputList){
List valueList=新的ArrayList();
for(字符串键:record.keySet()){
valueList.add(record.get(key));
}
系统输出打印项次(价值清单);
csvPrinter.printRecord(价值清单);
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 | type | value | key4 | excel | 0 | excel.sheet.no5 | excel | dd/MM/yyyyy | excel.date.format6 | excel | 0 | excel.baserate.rownum

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

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

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

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

注意:此设置仅在打印期间使用,不影响解析。分析当前仅适用于带有“\n”、““\r”和“\r\n”的输入


我有同样的问题,用RecordSeparator更改没有帮助。对于解决方案,我直接打印新行,如下所示:

private static final char DELIMITER_CHAR = ',';

        try (CSVParser parser = CSVFormat.newFormat(DELIMITER_CHAR).withTrailingDelimiter(false)
                .withRecordSeparator('\n').parse(new InputStreamReader(new ByteArrayInputStream(bytes)));
             CSVPrinter printer = CSVFormat.newFormat(DELIMITER_CHAR).print(destinationFile, Charset.defaultCharset())) {
            for (CSVRecord record : parser) {
                try {
                    printer.printRecord(record);
                   // printer.println(); // TODO not working
                   printer.print('\n'); // work around solution 
                } catch (Exception e) {
                    throw new RuntimeException("Error at line " + parser.getCurrentLineNumber(), e);
                }
            }
            printer.flush();
        }

非常感谢您的回复。现在我明白为什么不打印新行了。在我修改代码之后,我有两个问题。新代码:csvPrinter=New csvPrinter(新文件编写器(输出文件),CSVFormat.DEFAULT.withDelimiter(delimiter.charAt(0)).withHeader(header));1.标题用双引号打印。2.文件末尾还有一个空行。
private static final char DELIMITER_CHAR = ',';

        try (CSVParser parser = CSVFormat.newFormat(DELIMITER_CHAR).withTrailingDelimiter(false)
                .withRecordSeparator('\n').parse(new InputStreamReader(new ByteArrayInputStream(bytes)));
             CSVPrinter printer = CSVFormat.newFormat(DELIMITER_CHAR).print(destinationFile, Charset.defaultCharset())) {
            for (CSVRecord record : parser) {
                try {
                    printer.printRecord(record);
                   // printer.println(); // TODO not working
                   printer.print('\n'); // work around solution 
                } catch (Exception e) {
                    throw new RuntimeException("Error at line " + parser.getCurrentLineNumber(), e);
                }
            }
            printer.flush();
        }