4

What is the difference between newLine() and carriage return ("\r")? Which one is best to use?

File f = new File(strFileGenLoc);
BufferedWriter bw = new BufferedWriter(new FileWriter(f, false));
rs = stmt.executeQuery("select * from jpdata");
while ( rs.next() ) 
{
    bw.write(rs.getString(1)==null? "":rs.getString(1));
    bw.newLine();
}
james.garriss
  • 12,959
  • 7
  • 83
  • 96
Manu
  • 3,179
  • 23
  • 57
  • 69

5 Answers5

14

Assuming you mean this:

public static String newLine = System.getProperty("line.separator");

newLine is environment agnostic \r isn't.

So newLine will give you \r\n on windows but \n on another environment.

However, you shouldn't use this in a JTextArea and println will work fine with just \n on windows.

Edit now that I've seen the code and your comment

In your situation. I think you should use your own constant - \r\n

  File f = new File(strFileGenLoc);
  BufferedWriter bw = new BufferedWriter(new FileWriter(f, false));
  rs = stmt.executeQuery("select * from jpdata");
  while ( rs.next() ) {
    bw.write(rs.getString(1)==null? "":rs.getString(1));
    bw.write("\\r\\n");
  }
Rob Stevenson-Leggett
  • 35,279
  • 21
  • 87
  • 141
  • . i am creating text file in unix environment using java code. and transmitting the file to client ,but from thier side they need carriage return in text file. which one i have to use newLine() or carriage return ("\r") – Manu Jul 22 '10 at 10:41
  • 1
    I would use "\r\n" as that will display properly on windows and most unix text editors. – Rob Stevenson-Leggett Jul 22 '10 at 10:59
9

In the old days of ASR-33 teletypes (and, later, dot-matrix printers with travelling print-heads), the CR literally returned the carriage to the left, as in a typewriter, and the LF advanced the paper. The machinery could overlap the operations if the CR came before the LF, so that's why the newline character was always CR-LF, i.e. \r\n. If you got it back to front it took much longer to print. Unix was the first (only?) system to adopt just \n as the standard line separator. DOS/Windows didn't.

user207421
  • 305,947
  • 44
  • 307
  • 483
1

Write a line separator. The line separator string is defined by the system property line.separator, and is not necessarily a single newline ('\n') character.

source: http://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/api/java/io/BufferedWriter.html

Ahmed Aman
  • 2,373
  • 1
  • 19
  • 33
0

the major newLine(\n) and carriage return (\r) is :

new line(\n) & carriage return (\r) both r escape sequence in java..

new line(\n):

when ever \n is found in executable statements,the JVM splits the line into 2 parts from where we placed \n (escape sequence).. example :

println("Stackover\nflow");

output:

Stackover // here the line got split into 2 lines because of "\n"   
flow

carriage return (\r):

the data or character placed after the \r get overtire with the previous characters present in the same line ...

example :

println("Stackover\rflow"); 
output:
flow  // here Statckover is overtired  because of "\r" 
HighEnergy
  • 47
  • 12
charan
  • 11
  • The JVM doesn't split anything. The *line* is *already split,* by virtue of the newline. The *device* it's being printed *on* may or may not behave as you've described. The JVM plays precisely no part in any of it. Too much misinformation. -1 – user207421 Mar 13 '14 at 01:10
0

On Windows platforms, the line separator is CRLF (carriage return / line feed), which is "\r\n". On Unix platforms, it's just LF, "\n". Traditionally, on Apple platforms it has been just CR, "\r".

Zay Ya
  • 195
  • 1
  • 4
  • 15