Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I force Windows line endings in Java app?

Tags:

java

file

io

I have a Java app that writes to a file with:

BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile));
bw.write(line + lineTermination);

Line termination is defined as:

\r\n

I get the odd, mysterious blank line inserted into my file.

I get no extra lines if I change my code to:

bw.write(line);
bw.newLine();

But I want to force a specific line ending, not use System property. Clients specifically request a line ending character - some even have |. Its not a viable fix to just use \n.

Here is an snippet of the data with missing line:

"KABE","14/01/11","14:35","14:56","1987","US","SS","CO","MARRIED WITH CHILDREN","","EINE SCHRECKLICH NETTE FAMILIE","","N","10","","12","O'NEILL ED","13","SAGAL KATEY"

"PRO7","14/01/11","14:35","14:55","2001","US","SS","CO","SCRUBS","","SCRUBS DIE ANFAENGER","","C","10","BERNSTEIN ADAM","12","BRAFF ZACH","13","CHALKE SARAH"

Thanks for your time :-)

like image 897
mryan Avatar asked May 25 '11 17:05

mryan


1 Answers

You can call

System.setProperty("line.separator", "\r\n");

in order to set the system property inside your code.

like image 147
Howard Avatar answered Sep 26 '22 15:09

Howard