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? 开发者_JAVA百科"":rs.getString(1));
bw.newLine();
}
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");
}
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.
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
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".
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"
精彩评论