Ben Chuanlong Du's Blog

It is never too late to learn.

BufferedReader in Java IO

Comemnt

  1. The methods BufferedRead.readLine and BufferedRead.lines are very helpful for reading text Files.

public String BufferedRead.readLine

Reads 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.

public Stream\<String> BufferedRead.lines

Returns a Stream, the elements of which are lines read from this BufferedReader. The Stream is lazily populated, i.e., read only occurs during the terminal stream operation. The reader must not be operated on during the execution of the terminal stream operation. Otherwise, the result of the terminal stream operation is undefined.

After execution of the terminal stream operation there are no guarantees that the reader will be at a specific position from which to read the next character or line.

If an IOException is thrown when accessing the underlying BufferedReader, it is wrapped in an UncheckedIOException which will be thrown from the Stream method that caused the read to take place. This method will return a Stream if invoked on a BufferedReader that is closed. Any operation on that stream that requires reading from the BufferedReader after it is closed, will cause an UncheckedIOException to be thrown.

In [ ]:
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));

Comments