Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 830
Simon Nickerson Avatar asked Apr 01 '09 15:04

Simon Nickerson


2 Answers

Try using the Scanner class:

String line = Scanner(file).useDelimiter("\r\n").next();
like image 140
Greg Noe Avatar answered Oct 02 '22 09:10

Greg Noe


Depending on what the use case for which you need the BufferedReader, you can maybe change over to using the Scanner class, which is able to read text from different sources (files, streams), and has a direct method th specify the delimiting pattern. See http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html#useDelimiter(java.lang.String)

like image 21
Juve Avatar answered Oct 02 '22 08:10

Juve