Is there an equivalent of BufferedReader.readLine() that lets me pick what my end of line characters are?

Simon Nickerson picture Simon Nickerson · Apr 1, 2009 · Viewed 8.5k times · Source

The Javadoc for BufferedReader.readLine() says:

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.

I need slightly better control than this (e.g. I would like to be able to specify that an end of line is "\r\n", so that an "\n" by itself does not terminate the line).

Is there any JDK or library function which does this?

Answer

Greg Noe picture Greg Noe · Apr 1, 2009

Try using the Scanner class:

String line = Scanner(file).useDelimiter("\r\n").next();