CharArrayReader class

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

This class implements a character buffer that can be used as a character-input stream.

ConstructorSummary
CharArrayReader(char[] buf)Creates a CharArrayReader from the specified array of chars.
CharArrayReader(char[] buf, int offset, int length)Creates a CharArrayReader from the specified array of chars.

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.
intread(char[] b, int off, int len)Reads characters into a portion of an array.
booleanready()Tells whether this stream is ready to be read.
voidreset()Resets the stream to the most recent mark, or to the beginning if it has never been marked.
longskip(long n)Skips characters.
Revised from Open JDK source code

import java.io.CharArrayReader;
import java.io.IOException;

public class Main {
  public static void main(String args[]) throws IOException {
    CharArrayReader inStream;
    inStream = new CharArrayReader(new char[] { 'j', 'a', 'v', 'a', '2', 's', '.', 'c', 'o', 'm' });
    int ch = 0;
    StringBuffer sb = new StringBuffer("");
    while ((ch = inStream.read()) != -1)
      sb.append((char) ch);
    String s = sb.toString();
    System.out.println(s.length() + " characters were read");
    System.out.println("They are: " + s);

  }
}

The output:


10 characters were read
They are: java2s.com
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.