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 theString
and returns it as an integer value. If the end of theString
has been reached, it returns -1.read(char[] cbuf, int off, int len)
: This method reads characters from theString
into a character arraycbuf
. Theoff
parameter specifies the offset within the array at which to start storing the characters, andlen
specifies the maximum number of characters to read.skip(long n)
: This method skipsn
characters in theString
.
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:
StringReader(String s)
: This is the constructor for theStringReader
class that takes aString
parameters
and creates a newStringReader
object that can read characters from the givenString
.int read() throws IOException
: This method reads a single character from theString
and returns it as an integer value. If the end of theString
has been reached, it returns -1.int read(char[] cbuf, int off, int len) throws IOException
: This method reads characters from theString
into a character arraycbuf
. Theoff
parameter specifies the offset within the array at which to start storing the characters, andlen
specifies the maximum number of characters to read.boolean ready() throws IOException
: This method checks whether theStringReader
is ready to be read. It returnstrue
if theString
is not empty and there are characters available to be read, otherwise it returnsfalse
.void close() throws IOException
: This method closes theStringReader
and releases any system resources associated with it.long skip(long n) throws IOException
: This method skipsn
characters in theString
. It returns the number of characters skipped, which may be less thann
if the end of theString
is reached beforen
characters have been skipped.boolean markSupported()
: This method checks whether themark()
andreset()
methods are supported by theStringReader
. It returnstrue
if these methods are supported, otherwise it returnsfalse
.void mark(int readAheadLimit) throws IOException
: This method marks the current position in theString
. ThereadAheadLimit
parameter specifies the maximum number of characters that can be read after the mark before the mark becomes invalid.void reset() throws IOException
: This method resets theStringReader
to the position marked by themark()
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.