Java Reference ยท I/O and Files
The BufferedReader class
Reference for java.io.BufferedReader: read text line by line with readLine and lines, wrap files and System.in, and avoid common close and charset mistakes.
BufferedReader wraps another Reader and adds a buffer, so that reading characters from a file, a socket or the console does not hit the underlying source one character at a time. Its signature feature is readLine(), which returns the next line of text without its terminator, or null at the end of the input.
You rarely construct it from a FileReader any more. For files, Files.newBufferedReader(path) is shorter and defaults to UTF-8. For the console, wrap new InputStreamReader(System.in). For small files, Files.readAllLines is simpler still.
try (BufferedReader in = Files.newBufferedReader(path)) { String line; while ((line = in.readLine()) != null) { // use line } }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
public class BufferedReader extends Reader BufferedReader(Reader in) BufferedReader(Reader in, int size)
| Member | Description |
|---|---|
String readLine() |
Next line without terminator; null at end of input. |
int read() |
One character as an int, or -1 at the end. |
int read(char[] buf, int off, int len) |
Fills part of an array; returns the count or -1. |
Stream<String> lines() |
A lazy stream of the remaining lines. |
boolean ready() |
true if the next read will not block (the buffer is non-empty or the source has data). |
void mark(int limit) / reset() |
Remember a position and return to it, as long as you read fewer than limit characters in between. |
long skip(long n) |
Skips characters. |
void close() |
Closes this reader and the wrapped one. |
Throws: IOException from every read method. reset() throws IOException when the mark is invalid.
Usage notes
- Line terminators.
readLine()treats\n,\rand\r\nas line ends, and never includes them in the result. You cannot tell from the result which one was used. - A last line without a newline is still returned; an empty final line is not produced.
- The end is
null, not an empty string. A blank line returns"". - Charset. Pass an explicit
CharsettoInputStreamReaderor useFiles.newBufferedReader.new FileReader(f)uses the default charset (UTF-8 from Java 18 on). - Always close it with try-with-resources, which closes the wrapped reader too. Do not close one that wraps
System.inif you will read standard input again later. - Buffer size defaults to 8192 characters, which is fine for almost everything.
- Thread safety: it is synchronized, but sharing one reader between threads still makes line order unpredictable.
ScannerversusBufferedReader:Scannerparses tokens (numbers, words) and is more convenient;BufferedReaderis simpler and noticeably faster for large inputs.lines()should be closed together with the reader and throwsUncheckedIOExceptionon read errors.