Java-CharArrayReader

CharArrayReader

CharArrayReader class contains an internal buffer that can be used as a character-input stream. It inherits Reader class.

CharArrayReader Constructors

Constructors Description
CharArrayReader(char[] buf) Creates a CharArrayReader from a specified character array.
CharArrayReader(char[] buf, int offset, int length) Creates a CharArrayReader from a specified part of character array.

CharArrayReader Methods

Method Description
int read() Reads a single character.
int read(char[ ] cb) Reads up to cb.length characters from this stream into an array of characters.
int read(char[ ] cb, int off, int len) Reads up to len characters from this stream into an array of characters.
int read(CharBuffer cb) Attempts to read characters into the specified character buffer.
boolean  markSupported() Tells whether this stream supports the mark() operation, which it does.
void        mark(int readAheadLimit) Sets the current marked position in the stream. The parameter gives the maximum limit of bytes that can be read before the marked position becomes invalid.
void        reset() Resets the stream to the most recent mark, or to the beginning if it has never been marked.
boolean  ready() Tells whether this stream is ready to be read.
long        skip(long n) Skips characters.
void        close() Closes the stream and releases any system resources associated with it.

Program

CharArrayReader

Program Source

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

public class Javaapp {
    
    public static void main(String[] args) {
        
        String str = "ProgramC++RubyJavaPHP";
        try(CharArrayReader chr = new CharArrayReader(str.toCharArray())){
            
            chr.skip(14);
            char ca1[] = new char[4];
            chr.read(ca1, 0, 4);
            for(char c : ca1)
            {
                System.out.print(c);
            }
            
            chr.reset();
            char ca2[] = new char[7];
            chr.read(ca2, 0, 7);
            for(char c : ca2)
            {
                System.out.print(c);
            }
        }catch(IOException ex){
            
            System.out.println("An I/O Error Occurred");
        }      
    }
}

Leave a Comment