2

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?

2
  • A single blank line between data is not valid CSV. How would you parse that? Commented Mar 21, 2018 at 13:21
  • 1
    And don't think of that output as "two blank lines", you're getting one "Record" that's a newline character surrounded by quotes Commented Mar 21, 2018 at 13:23

4 Answers 4

3

printRecord prints a record separator after the data, in this case a newline.

Then you print a newline which is printed as a string containing another newline, which gives you those two double quotes on separate lines.

Just remove the one that prints a newline or, if you really want that empty line, use csvFilePrinter.println() which will output the record separator (in your case, "\n").

Sign up to request clarification or add additional context in comments.

2 Comments

Removing it doesn't create a single blank line in the output, as asked, though
CSVFormat has an option to ignore blank lines though
2

Try csvFilePrinter.printRecord(""); csvFilePrinter.println(); printRecord will escape the line automatically. You do not need an additional escape character.

2 Comments

This would still put two quotes on a single line, no?
Yes.. "" are still present at the blank line
1

I guess replacing csvFilePrinter.printRecord("\n"); with csvFilePrinter.println(); does the job. println() will output lines separator, so you will get completely empty line without quotes.

Hope it helps!

3 Comments

This would still put two quotes on a single line, no?
This worked.. But as @cricket_007 said I am getting following output: data1 "" data 2
@RadhikaKulkarni, I've updated my answer after your clarification.
0

You define the CSVFilePrinter with

CSVFormat.DEFAULT.withRecordSeparator("\n")

so for each record an additional \n will be appended. You then print three records: "data1", "\n", "data2"

This will results in "data1\n\n\ndata2\n", which is exactly the result you get.

Just replace the second record with "".

2 Comments

I agree with your logic. I have 1 question. Why " is displayed at those blank lines?
@RadhikaKulkarni You're right, I can't explain that. I thought that you just wanted to emphasize the empty lines. Is it a single double quote (") or two single quotes ('')? In the latter case, this might be to denote an empty value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.