Manu Manu - 4 months ago 30
Java Question

difference between newLine() and carriage return("\r")

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();
}

Answer

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");
  }
Comments