Java Reader Class
What is Java Reader Class?
- In Java, Reader is an abstract class that provides a standard way to read characters from a stream of data.
- It is the base class for all character input stream classes, such as FileReader, StringReader, and InputStreamReader.
- It is a part of the Java IO API, which provides a set of classes and interfaces for performing input and output operations.
- To understand the Java Reader Class, Read the Complete Article.
Subclasses of Reader:
- Here are some of the commonly used subclasses of Reader in Java:
- BufferedReader: A Reader subclass that efficiently reads characters from a character stream by buffering them. It offers strategies for reading entire lines of text at once as opposed to single characters.
- InputStreamReader: A subclass of Reader that reads characters from an input stream while decoding the characters it encounters. It is frequently used to read text from a network socket or standard input.
- CharArrayReader: A Reader subclass that reads characters from a character array that is stored in memory. It is helpful when reading characters from a String or char array without first constructing a new String object.
- StringReader: A subclass of Reader that reads characters from a String. It is similar to CharArrayReader, but takes a String object as input instead of a char array.
- PipedReader: A subclass of Reader that provides a way to connect two threads through a pipe. One thread writes data to a PipedWriter, while the other thread reads data from a PipedReader.
These subclasses provide additional functionality and convenience methods on top of the basic methods defined in the Reader class, making it easier to read characters from different types of sources.
Methods of Reader:
The Reader class defines several abstract methods that must be implemented by its subclasses:
read(char[] cbuf, int off, int len): Reads up to len characters into the given character array cbuf, starting at the offset off, and returns the number of characters read. If no characters are available, returns -1.
skip(long n): Skips over n characters in the input stream. Returns the actual number of characters skipped.
close(): Closes the input stream and releases any system resources associated with it.
mark(int readAheadLimit): Marks the current position in the input stream, up to a maximum of readAheadLimit characters. Subsequent calls to reset() will reposition the stream to this point.
reset(): Resets the input stream to the last marked position.
Let’s look at the Java Reader Class to perform certain operations.
Example 1: Reader using ReaderFile
import java.io.IOException; import java.io.Reader; import java.io.StringReader; public class Main { public static void main(String[] args) { String data = "This is a test string."; try (Reader reader = new StringReader(data)) { int character; while ((character = reader.read()) != -1) { System.out.print((char) character); } } catch (IOException e) { System.err.println("Error reading data: " + e.getMessage()); } } }
Output
This is a test string.
Explanation:
We use a try-with-resources statement to automatically close the Reader object when we're done with it.
Inside the try block, we use a while loop to read characters from the Reader object one at a time, using the read() method.
We convert the int value returned by read() to a char using a cast, and then print it to the console using System.out.print().
When we're done reading data, the Reader object is automatically closed when the try block exits.
Example 2 : Reader Using FileReader
import java.io.IOException; import java.io.Reader; import java.io.StringReader; public class Main { public static void main (String[]args) { String data = "PrepInsta Prime is the New Netflix."; try (Reader reader = new StringReader (data)) { int character; while ((character = reader.read ()) != -1) { System.out.print ((char) character); } } catch (IOException e) { System.err.println ("Error reading data: " + e.getMessage ()); } } }
Output
PrepInsta Prime is the New Netflix.
Explanation:
We use a try-with-resources statement to automatically close the Reader object when we're done with it.
Inside the try block, we use a while loop to read characters from the Reader object one at a time, using the read() method.
We convert the int value returned by read() to a char using a cast, and then print it to the console using System.out.print().
When we're done reading data, the Reader object is automatically closed when the try block exits.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment