Carriage return and new line with Java and readLine()

From the javadocs:

public String readLine() throws IOException

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.
Returns:
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
Throws:
IOException - If an I/O error occurs


If rd is of type BufferedReader there is no way to figure out if readLine() returned something that ended with \n, \r or \r\n... the end-of-line characters are discarded and not part of the returned string.

If you really care about these characters, you can't go through readLine(). You'll have to for instance read the characters one by one through read().

Tags:

Java