I'm new to apache-commons-csv 1.6
I have a basic requirement to print csv file with every record in new line. Am trying to use CSVPrinter's println method. For some weird reason, the output file doesn't have any newline character. Everything is printed in one single line.
I have tried to open the output in Notepad++ and show the non-printable characters. There is no character between the records. Any help would be appreciated greatly. Thanks.
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();
Expected result:
id|type|value|key
4|excel|0|excel.sheet.no
5|excel|dd/MM/yyyy|excel.date.format
6|excel|0|excel.baserate.rownum
Actual result: id|type|value|key4|excel|0|excel.sheet.no5|excel|dd/MM/yyyy|excel.date.format6|excel|0|excel.baserate.rownum
Don't use newFormat method if you don't want to override all delimiters
Use this method if you want to create a CSVFormat from scratch. All fields but the delimiter will be initialized with null/false.
If you want to add new line delimiter per record add 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"