# Java Programs
➤ File Class & Methods
➤ Create File & Directories
➤ List Files & Directories
➤ Delete Files & Directories
➤ IOStream class & Methods
➤ Java OutputStream class
➤ InputStream class in Java
➤ FileOutputStream in Java
➤ Java FileInputStream class
➤ DataOutputStream class
➤ DataInputStream class
➤ Printstream class & Methods
➤ Java FileNotFoundException
➤ Writer Class & Methods
➤ Reader Class & Methods
➤ Java InputStreamReader
➤ FileWriter class & example
➤ FileReader class & example
➤ BufferedWriter class in Java
➤ BufferedReader class in Java
➤ PrintWriter class & methods
The reader class in Java is an abstract class defined in java.io package and superclass for all character input streams. The reader class and all its subclasses were introduced in Java 1.1. It implements Readable, and Closeable marker interfaces. Reader interface defines read() method and Closable interface defines close() method.
public abstract class Reader
implements Readable, Closeable
All subclass of Reader are used to read the character/text data from the file or keyboard or network.
Java Reader Class Methods
In Reader class there are different overloaded read() methods are given,
|Modifier and Type||Method||Desription|
|int||It reads a single character at a time.|
|int||It reads characters, stores them to the passed char array, and returns the number of characters it read.|
|abstract int||It reads characters from |
|int||It read characters into the specified character buffer. Return number of characters added to the buffer.|
All overloaded forms of the read methods will read data until some input is available, an I/O error occurs, or the end of the stream is reached. They return -1 when the end of the stream has been reached.
|Modifier and Type||Method||Descritption|
|boolean||Tells whether the stream is ready to be read.|
|long||It skip number of given characters.|
|boolean||Tells whether this stream supports the mark() operation. Default implementaion always returns false.|
|void||It marks the present position in the stream.|
|void||It reset the stream.|
|Abstract void||It closes the stream and releases any system resources associated with it.|
It also have transferTo() method, which was introduced in Java 10 version.
|long||It reads all characters from the reader and writes the characters to the given writer in the order that they are read.|
The transferTo() method doesn’t close either the Reader and Writer. It rutruns NullPointerException if the passed Writer object out is null, otherwise it ruturns number of characters transfered.
Note:- Except markSupport() all other methods of java.io.Reader class throws
Since it is a connection based object and uses different resources of the computer. Therefore, after completion of the program, those connections should be closed and all related resources should be released. For this, close() is given and it is defined in the Closeable interface. The actual close() method is defined in the AutoCloseable marker interface which was introduced in Java 1.7 version. The Closeable interface is extended from the AutoClosable interface.
Subclasses of Reader
The LineNumberReader class is subclass of BufferedReader, PushBackReader is the sub class of FilteredReader, and FileReader is the subclass of InputStreamReader. The BufferedReader, InputStreamReader and FileReader are the most important sub classes of the Reader.
None of these subclasses having 0 parameter constructor because it is a stream-based connection where one end is Java application. Therefore second end information must be passed as input to the constructor.
Among all these subclasses only FileReader class can directly communicate with files. FileReader class has several constructors with File as parameters. Similarly, only InputStreamReader can directly communicate to the console. The remaining subclasses are dependent on the FileReader class to communicate with the file, dependent on InputStreamReader to communicate with the console.
The InputStreamReader is a bridge from byte streams to character streams. It reads bytes and decodes them into characters using a specified charset.
Among these all subclasses BufferedReader is the most convenient way to read character or text data. It has a method to read one line at a time, which makes our tasks very easy compare to other classes.
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!