I want to insert a blank line in csv using java. I have written following code:
CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator("\n").withEscape('\\');
FileWriter fileWriter = new FileWriter(csvFile);
CSVPrinter csvFilePrinter = new CSVPrinter(fileWriter,csvFileFormat);
csvFilePrinter.printRecord("data1");
csvFilePrinter.printRecord("\n");
csvFilePrinter.printRecord("data2");
When I open this generated CSV in notepad++, I am getting 2 blank lines:
data1
"
"
data2
How can I get a single blank line?
Try csvFilePrinter.printRecord("");
csvFilePrinter.println();
printRecord
will escape the line automatically. You do not need an additional escape character.