Java strings and line termination

Status
Not open for further replies.

Jeff S

Ars Legatus Legionis
11,958
Subscriptor++
I've got a simple Java program I wrote, that does some basic text output, using System.out.printf(). In my strings, I use \n to terminate the lines. I'm pretty sure that in the past, when I ran my Java program, the output had correct line termination on Windows.

But, today I re-ran my program, and the printf statements are not properly terminating the lines (e.g. outputting \r\n on Windows).

Is there some sort of JVM setting which controls this? What is the 'proper' way to terminate lines within strings, so that regardless of what platform you run the program on, Java will emit the correct line terminator? I had been under the impression that Java was supposed to translate the \n automatically into the correct line terminator - is that not correct?
 

Jeff S

Ars Legatus Legionis
11,958
Subscriptor++
I was just reading the Java documentation, and according to the page documenting the "Format String" class ( http://download.oracle.com/javase/6/doc ... tml#syntax ), for the \n sequence, it says, "The result is the platform-specific line separator".

Any idea why it's not working? I should probably mention that my java program is being called from a windows batch file, with output redirection for standard output into a text file, e.g.:

java.exe {options} class >>%1

(where %1 is the name of the text file passed as an argument to the batch file to append output to).

Is it possible that when invoked with output redirection like this, Java behaves differently than if you invoke Java without redirection?
 
Status
Not open for further replies.