StringReader class

                                 
    java.lang.Object                            
     |                           
     |--java.io.Reader                        
         |                       
         |--java.io.StringReader                    
                                 

A character stream whose source is a string.

ConstructorSummary
StringReader(String s)Creates a new string reader.

ReturnMethodSummary
voidclose()Closes the stream and releases any system resources associated with it.
voidmark(int readAheadLimit)Marks the present position in the stream.
booleanmarkSupported()Tells whether this stream supports the mark() operation, which it does.
intread()Reads a single character. The character read, or -1 if the end of the stream has been reached.
intread(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.
booleanready()Tells whether this stream is ready to be read.
voidreset()Resets the stream to the most recent mark, or to the beginning of the string if it has never been marked.
longskip(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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.