Java StringReader Class

The StringReader class in Java is a subclass of the abstract class Reader that allows you to read character streams from a String object. It is particularly useful when you want to read from a String as if it were a stream, without the need to create a file or use any other input/output (I/O) stream.

To use the StringReader class, you first need to create an instance of the class and pass the String object that you want to read from as an argument to its constructor. For example:

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

Once you have created an instance of StringReader, you can use its methods to read characters from the String. The most commonly used methods are:

  • read(): This method reads a single character from the String and returns it as an integer value. If the end of the String has been reached, it returns -1.
  • read(char[] cbuf, int off, int len): This method reads characters from the String into a character array cbuf. The off parameter specifies the offset within the array at which to start storing the characters, and len specifies the maximum number of characters to read.
  • skip(long n): This method skips n characters in the String.

Here is an example of how to use the StringReader class:

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

int c;
while ((c = reader.read()) != -1) {
    System.out.print((char) c);
}

This code reads each character from the String “Hello World” using the read() method of the StringReader class and prints it to the console. The output of this code would be:

Hello World

Java StringReader class declaration:

The StringReader class in Java is part of the java.io package, and its declaration is as follows:

public class StringReader extends Reader {
    // constructors
    public StringReader(String s);
    
    // methods
    public int read() throws IOException;
    public int read(char[] cbuf, int off, int len) throws IOException;
    public boolean ready() throws IOException;
    public void close() throws IOException;
    public long skip(long n) throws IOException;
    public boolean markSupported();
    public void mark(int readAheadLimit) throws IOException;
    public void reset() throws IOException;
}

As mentioned before, the StringReader class is a subclass of the Reader class, which means it inherits all the methods of Reader class, such as ready(), close(), mark(), reset() etc.

In addition to the inherited methods, the StringReader class has its own constructor and methods that are specific to reading from a String object. The constructor accepts a String parameter, which is the String object to be read from, and creates an instance of the StringReader class.

The read() method of StringReader reads a single character from the String and returns it as an integer value. The read(char[] cbuf, int off, int len) method reads characters from the String into a character array cbuf, starting at the specified offset off and reading up to len characters. The skip() method skips n characters in the String.

The markSupported(), mark(), and reset() methods of StringReader provide support for marking a position in the stream and returning to it later.

Overall, the StringReader class is a useful tool for reading character streams from a String object in Java.

Methods of StringReader class:

The StringReader class in Java provides several methods that allow you to read character streams from a String object. These methods include:

  1. StringReader(String s): This is the constructor for the StringReader class that takes a String parameter s and creates a new StringReader object that can read characters from the given String.
  2. int read() throws IOException: This method reads a single character from the String and returns it as an integer value. If the end of the String has been reached, it returns -1.
  3. int read(char[] cbuf, int off, int len) throws IOException: This method reads characters from the String into a character array cbuf. The off parameter specifies the offset within the array at which to start storing the characters, and len specifies the maximum number of characters to read.
  4. boolean ready() throws IOException: This method checks whether the StringReader is ready to be read. It returns true if the String is not empty and there are characters available to be read, otherwise it returns false.
  5. void close() throws IOException: This method closes the StringReader and releases any system resources associated with it.
  6. long skip(long n) throws IOException: This method skips n characters in the String. It returns the number of characters skipped, which may be less than n if the end of the String is reached before n characters have been skipped.
  7. boolean markSupported(): This method checks whether the mark() and reset() methods are supported by the StringReader. It returns true if these methods are supported, otherwise it returns false.
  8. void mark(int readAheadLimit) throws IOException: This method marks the current position in the String. The readAheadLimit parameter specifies the maximum number of characters that can be read after the mark before the mark becomes invalid.
  9. void reset() throws IOException: This method resets the StringReader to the position marked by the mark() method.

Overall, the methods of the StringReader class provide a convenient way to read character streams from a String object in Java.

Java StringReader Example:

Here is an example of using the StringReader class in Java:

import java.io.*;

public class StringReaderExample {
    public static void main(String[] args) {
        String s = "Hello, World!";
        StringReader reader = new StringReader(s);
        try {
            int c = reader.read();
            while (c != -1) {
                System.out.print((char) c);
                c = reader.read();
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In this example, we create a String object s and a StringReader object reader that reads from s. We then use a while loop to read each character from the StringReader and print it to the console using System.out.print(). Finally, we close the StringReader.

When we run this program, the output will be:

Hello, World!

This example demonstrates how to use the read() method of StringReader to read each character from the StringReader, and how to use a loop to continue reading until the end of the String is reached.