Java Reader

w‮ww‬.theitroad.com

In Java, Reader is an abstract class that provides a way to read characters from a stream. It is the character-based counterpart of the byte-based InputStream class. Reader defines a set of methods that can be used to read character data from a stream. Some commonly used methods of the Reader class are:

  1. read(): This method reads a single character from the stream and returns it as an integer.

  2. read(char[] cbuf): This method reads characters into an array and returns the number of characters read.

  3. read(char[] cbuf, int off, int len): This method reads characters into a portion of an array and returns the number of characters read.

  4. skip(long n): This method skips over a specified number of characters in the stream.

  5. mark(int readAheadLimit): This method marks the current position in the stream.

  6. reset(): This method resets the stream to the previously marked position.

  7. ready(): This method returns true if the stream is ready to be read.

  8. close(): This method closes the stream and releases any resources associated with it.

The Reader class is used extensively for processing character-based data in Java. The classes that extend from this abstract class include FileReader, StringReader, BufferedReader, InputStreamReader, and others.

FileReader is a class that reads characters from a file. StringReader is a class that reads characters from a string. BufferedReader is a class that reads text from a character input stream, buffering the characters to provide for the efficient reading of characters, arrays, and lines. InputStreamReader is a class for turning a byte stream into a character stream. It is often used to convert a InputStream to a Reader for reading text from a binary file.