Character I/O streams

Character I/O streams provide sequential input or output of characters. A stream can represent many different kinds of sources and destinations, including disk files and strings.

The Reader is root class for reading character streams. Main methods are read(), that allow to read one or more bytes from stream. When data are finished in stream, these methods return -1.

The Writer is root class for writing to character streams. Main methods are write(), that allow to write one or more characters to the stream.

You must close streams after using directly or use try-with-resource statement.

static void handleStrings(Reader reader, Consumer<String> handler) 
    throws IOException {
    
    BufferedReader br = (reader instanceof BufferedReader) ?
            (BufferedReader) reader : new BufferedReader(reader);

    String line = br.readLine();

    while (line != null) {
        handler.accept(line);         
        line = br.readLine();
    }
}

// convert input stream to the reader
// in our case input stream is Java standard input stream that used as console input 
BufferedReader in
   = new BufferedReader(new InputStreamReader(System.in));

character I/O streams overview

class description
Reader Root class of all classes representing an input stream of characters.
Writer Root class of all classes representing an output stream of characters.
InputStreamReader A bridge from byte streams to character streams. It reads bytes and decodes them into characters using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.
OutputStreamWriter A bridge from character streams to byte streams. Characters written to it are encoded into bytes using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.
FileWriter Writes text to character files using a default buffer size. Encoding from characters to bytes uses either a specified charset or the platform's default charset.
FileReader Reads text from character files using a default buffer size. Decoding from bytes to characters uses either a specified charset or the platform's default charset.
FileWriter Writes text to character files using a default buffer size. Encoding from characters to bytes uses either a specified charset or the platform's default charset.
CharArrayReader Implements a character buffer that can be used as a character input stream.
CharArrayWriter Implements a character buffer that can be used as an Writer. The buffer automatically grows when data is written to the stream. The data can be retrieved using toCharArray() and toString().
StringReader A character stream whose source is a string.
StringWriter A character stream that collects its output in a string buffer, which can then be used to construct a string.
BufferedReader Implements a character buffer that can be used as a character input stream. Also it has useful method readLine().
BufferedWriter Wraps other writer for buffering characters so as to provide for the efficient writing of single characters, arrays, and strings. The buffer size may be specified, or the default size may be accepted. The default is large enough for most purposes.

Reader

If need read lines, than wrap you reader by BufferedReader.

method description
close() Closes the stream and releases any system resources associated with it.
mark(aheadLimit) Marks the present position in the stream.
markSupported() Tells whether this stream supports the mark() operation.
read() Reads a single character.
read() Reads a single character.
read(buff) Reads characters into an array.
read(buff, offs, len) Reads characters into a portion of an array.
ready() Tells whether this stream is ready to be read.
skip(n) Skips characters. This method will block until some characters are available, an I/O error occurs, or the end of the stream is reached.
transferTo(out) Reads all characters from this reader and writes the characters to the given writer in the order that they are read.
static methods
nullReader() Returns a new Reader that reads no characters.

Writer

method description
append(ch) Appends the specified character.
append(ch_seq) Appends the specified character sequence.
append(ch_seq, start, end) Appends a subsequence of the specified character sequence to this writer.
append(ch_seq, start, end) Appends a subsequence of the specified character sequence to this writer.
close() Closes the stream, flushing it first.
flush() Flushes the stream. If the stream has saved any characters from the various write() methods in a buffer, write them immediately to their intended destination.
write(ch) Writes a single character.
write(buff) Writes an array of characters.
write(buff, offs, len) Writes a portion of an array of characters.
write(str) Writes a string.
write(str, offs, len) Writes a portion of a string.
static methods
nullWriter() Returns a new Writer which discards all characters.