Home Java Example Java Io Java IO CharArrayReader
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Java IO CharArrayReader

Advertisement
In this tutorial we will learn about the CharArrayReader class in Java.

Java IO CharArrayReader

In this tutorial we will learn about the CharArrayReader class in Java.

CharArrayReader class in java.io package provides the facility to read the character input stream from char array. Input stream for this class is an array of characters.

Constructor of CharArrayReader

Constructor Name
Description
CharArrayReader(char[] buf) This constructor creates a CharaArrayReader with a specified char array data source.
Syntax : public CharArrayReader(char[] buf)
CharArrayReader(char[] buf, int offset, int length) This constructor creates a CharArrayReader with the specified length of character of a char array started from the specified offset 'off'.
Syntax : public CharArrayReader(char[] buf, int offset, int length)

Methods in CharArrayReader

  • close() : This method is used to close the stream as well as to release the resources associated with the stream.

    Syntax : public void close()
     
  • mark(int readAheadLimit) : This method is used to mark the current position in the stream.

    Syntax : public void mark(int readAheadLimit) throws IOException
     
  • markSupported() : This method returns a boolean value which specifies that whether this stream supported mark() method or not.

    Syntax : public boolean markSupported()
     
  • read() : This method is used to read a single character from the stream.

    Syntax : public int read() throws IOException
     
  • read(char[] b, int off, int len) : This method is used to read the specified length of character at the specified position from a specified char array.

    Syntax : public int read(char[] b, int off, int len) throws IOException
     
  • ready() : This method returns a boolean value which specifies whether this stream is ready to use or not.

    Syntax : public boolean ready() throws IOException
     
  • reset() : This method resets the marked position.

    Syntax : public void reset() throws IOException
     
  • skip(long n) : This method is used to skip the specified number of characters.

    Syntax : public long skip(long n) throws IOException

Example

A simple example is being given here which demonstrates about how to use the java.io.CharArrayReader to read the stream of char array. In this example I have created a Java class to read the char array. In this class to read the char array creates a char array variable with some characters then passes this input stream to the constructor of CharArrayReader().

Source Code

JavaCharArrayReaderExample.java

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

public class JavaCharArrayReaderExample
{
   public static void main(String args[])
    {
       char ch[] = {'a', 'b', 'c', 'd'};
       CharArrayReader car = null;
       try
         {
            car = new CharArrayReader(ch);
            int c;
            System.out.println();
            System.out.println("Characters in char array");
             while((c=car.read()) != -1)
               {
	     System.out.print("'"+(char)c+"'"+" ");           
               }          
         }
        catch(Exception e)
         {
            System.out.println(e);
         }
        finally
         {
            if(car != null)
            {
               try
                 {
                    car.close();
                 }
               catch(Exception ioe)
                 {
                     System.out.println(ioe);
                 }
            }
         }// end finally
    }// end main
}// end class

Output

Download Source Code

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 15, 2012

Ask Questions?    Discuss: Java IO CharArrayReader  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com