Java StringReader Class

Java StringReader Class

What are StringReader Class?

In the Article, we will Discuss about the StringReader class of java.
In Java, the StringReader class is a subclass of the abstract class Reader that reads character streams from a string.It takes a string as an input and provides methods for reading the characters from the string.

Java StringReader Class:

In Java, the StringReader class is a subclass of the Reader abstract class that allows you to read character streams from a String object. The StringReader class provides methods to read characters from the string and perform other operations on the input stream.

The StringReader class is useful when you want to read data from a string in the same way you would read data from a file or network stream. You can use it to read a string as a stream of characters and perform various operations on the stream, such as skipping characters or marking a position in the stream.

The StringReader class provides a simple and efficient way to process character data stored in a String object, without the need for additional I/O resources such as files or network connections. It is often used in combination with other Java I/O classes to manipulate and process text data.

Creating a StringReader in Java:

To create a StringReader object in Java, you can use one of its constructors that takes a String parameter. Here’s an example:

Syntax:

String str = "Hello, World!";
StringReader reader = new StringReader(str);

Explanation:

In the above example, we create a String object that contains the string “Hello, World!”, and then pass it to the StringReader constructor to create a StringReader object. The StringReader object is assigned to the variable reader.

Some common Methods of StringReader in Java:

Java Example Program For StringReader Class:

Run
// Importing the required packages
import java.io.StringReader;
import java.io.IOException;

public class Main{
    public static void main(String[] args){
        // Initializing the String
        String str = "Hello, World!";
        
        // Initializing the String Reader class Variable
        StringReader reader = new StringReader(str);

        try {
            int c;
            while ((c = reader.read()) != -1) {
                System.out.print((char) c);
            }
        } 
        catch (IOException e) {
            e.printStackTrace();
        } 
        finally {
            reader.close();
        }
    }
}

Output:

Hello, World!

Explanation:

In the above program, we create a StringReader object from the String “Hello, World!”, and then use a while loop to read characters from the input stream using the read() method. The while loop continues until the end of the stream is reached, which is indicated by the read() method returning -1. Each character is printed to the console using the System.out.print() method.

Note that the read() method may throw an IOException, so we include it in a try-catch block to handle any exceptions that may occur. Finally, we call the close() method to release any resources associated with the StringReader object.

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

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription