This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes linebreaks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "linebreaks" Watch "linebreaks" New topic


Csaba Kassitzky
Ranch Hand

Joined: Nov 29, 2005
Posts: 38
Hi everyone!

Someone mentioned \n and \r are the linebreaks.
Can "\n\r" be used both? Does this count as one linebreak?

C Broussard

Joined: Jun 18, 2004
Posts: 28
i usually just do "\r\n"

not sure if switching works it.. you can write a quick static main and test it though...

public class test{

public static void main(String[] args){
System.out.print(" 1) this is a \r\n test ");
System.out.print(" 2) this is a \n\r test ");
System.out.print(" 3) this is a \n test ");
System.out.print(" 4) this is a \r test ");


just compile and run.
Csaba Kassitzky
Ranch Hand

Joined: Nov 29, 2005
Posts: 38
\r makes the whole line dissappear
Anyways, if i'm reading characters in, \r\n are 2 charaters right?
marc weber

Joined: Aug 31, 2004
Posts: 11343

See this thread...

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
When reading in a text file, it's more common to read it in one line at a time, versus one character at a time. For the former, BufferedReader has the method readLine:

Read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

The string readLine returns does not include any terminator.

But if you are reading one character at a time, "\r\n" is two chars.

There is no emoticon for what I am feeling!
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
See if this helps:

Tony Morris
Java Q&A (FAQ, Trivia)
I agree. Here's the link:
subject: linebreaks
Similar Threads
Need Printwriter help!!!!
newline characters
when and why we use FF and CR i,e.. \f and \r
Stripping Out Carriage Returns
How to insert new line character in string