The Java StringReader class represents a character stream that uses a string as its data source. It accepts a string input and converts it into a stream of characters. This class is a subclass of the Reader class.
The StringReader class does not utilize system resources such as network sockets or files, so it is not required to explicitly close the StringReader instance.
Java StringReader class declaration
Now, let's examine the declaration of the Java.io.StringReader class:
Example
public class StringReader extends Reader
Methods of StringReader class
| Method | Description |
|---|---|
| int read() | It is used to read a single character. |
| int read(char[] cbuf, int off, int len) | It is used to read a character into a portion of anarray. |
| boolean ready() | It is used to tell whether the stream is ready to be read. |
| boolean markSupported() | It is used to tell whether the stream support mark() operation. |
| long skip(long ns) | It is used to skip the specified number of character in a stream |
| void mark(int readAheadLimit) | It is used to mark the mark the present position in a stream. |
| void reset() | It is used to reset the stream. |
| void close() | It is used to close the stream. |
Java StringReader Example
Example
import java.io.StringReader;
public class StringReaderExample {
public static void main(String[] args) throws Exception {
String srg = "Hello Java!! \nWelcome to C# Programming.";
StringReader reader = new StringReader(srg);
int k=0;
while((k=reader.read())!=-1){
System.out.print((char)k);
}
}
}
Output:
Output
Hello Java!!
Welcome to C# Programming.