How do I force Windows line endings in Java app?
Asked Answered
P

1

5

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 :-)

Parotitis answered 25/5, 2011 at 17:4 Comment(3)
Are you sure that the line doesn't already have a line terminator? using write("something" + "\r\n") should force windows line endings.Diploid
String newLine = System.getProperty("line.separator").toString();Felizio
As it turns out I only get this mysterious line break in gedit. Ive learnt my lesson and updated my text editor to gVim. My original code works as expected. I should know by now to ALWAYS try in another browser/editor/relevant application!!!! Palm to face moment :-(Parotitis
C
9

You can call

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

in order to set the system property inside your code.

Cringle answered 25/5, 2011 at 17:10 Comment(5)
Testing it independent of what I am setting as the system property for new line it still doesn't influence the way a string is written into, and only \n gives a new line. I am on Win and I was trying \r\n and the result is as that of only \n.Clavate
@Clavate BufferedWriter.newLine uses exactly this property. "A newLine() method is provided, which uses the platform's own notion of line separator as defined by the system property line.separator." see here:Cringle
I agree it will set the character whatever you give when specifically called using newLine() but what I was expecting that it will also influence interpretation of String when writing it to a file. But I guess I wanted to much. :)Clavate
Is there not a way to force writing a \r without changing the system property for the entire application?Attainture
@Cringle and others: be advised that in the OpenJDK implementation, newLine() uses the value from System.lineSeparator(), which returns a cached value that in turn is read from the line.separator property once when the java.lang.System class is first loaded. Changing the system property afterwards thus has no effect on newLine() although it will of course affect any method that reads the property on each call. TL;DR: Launch with -Dline.separator=SomethingElse if you want to change this.Lapotin

© 2022 - 2024 — McMap. All rights reserved.