StringReader class
A character stream whose source is a string.
StringReader(String s)
- Creates a new string reader.
void close()
- Closes the stream and releases any system resources associated with it.
void mark(int readAheadLimit)
- Marks the present position in the stream.
boolean markSupported()
- Tells whether this stream supports the mark() operation, which it does.
int read()
- Reads a single character. The character read, or -1 if the end of the stream has been reached.
int read(char[] cbuf, int off, int len)
- Reads characters into a portion of an array. The character read, or -1 if the end of the stream has been reached.
boolean ready()
- Tells whether this stream is ready to be read.
void reset()
- Resets the stream to the most recent mark, or to the beginning of the string if it has never been marked.
long skip(long ns)
- Skips the specified number of characters in the stream.
Revised from Open JDK source code
import java.io.StringReader;
public class Main {
public static void main(String[] args) throws Exception{
StringReader reader = new StringReader("java2s.com");
int ch = 0;
while(true){
ch = reader.read();
if(ch == -1){
break;
}
System.out.println((char)ch);
}
}
}
The output:
j
a
v
a
2
s
.
c
o
m