Java.io - SPLessons

Java.io CharArrayReader

Home > Lesson > Chapter 8
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.io CharArrayReader

Java.io CharArrayReader

shape Introduction

The character array reader is the class used for reading of data in the form of character stream from the character array. The character buffer is used internally as an input stream for character arrays. But it doesn't use any type of resources like connection or file. The name of the class itself represents that have to read the character array as a stream.

Class Declaration

shape Declaration

Java.io CharArrayReader class is declared as shown below: Public class CharArrayReader extends Reader

shape Fields

Java.io CharArrayReader class fields are:
  • Protected int markedPos :The field represents the presently position that have been marked in the stream.
  • Protected int Count : The field represents to the index stream buffer ending.
  • Protected Char [] buf : The field represents to the internal buffer called character buffer where actually the data stored.
  • Protected int pos :The field represents to the position of input stream to read that coming character.

Class Constructors

shape Table

Constructor Description
CharArrayReader (Char[] buf) The function of this constructor is to create the character array reader and reads into the character buffer called array.
CharArrayReader (Char[] buf, int offset, int len) The function of this constructor is to create the character array reader with represented size of buffer.

Class Methods

shape Table

Method Description
void close () The function of this method is to close the present stream and associated to this stream, systems resources are released without any impact.
Long skip (long m) The function of this method is to skip the text or characters in this stream of ' m ' characters.
Boolean markSupported () The function of this method is to check the represented mark method is supported by this stream or not
int read(Char[] c, int off, int n) By using this method it reads the data up to 'n' bytes from the character stream into an array of characters.

Inherited Methods

shape Description

From the following classes, methods are inherited to the character array reader class.
  • Java.io.object
  • Java.io.Reader

shape Examples

Usage of void close() method. [c]import java.io.CharArrayReader; import java.io.IOException; public class CharArrayReader { public static void main(String[] args) { CharArrayReader car = null; char[] ch = {'S', 'P', 'L', 'E', 'S', 'S', 'O', 'N', 'S'}; try{ // creating a new character array reader car = new CharArrayReader(ch); // FOr to close the character array stream car.close(); // For To read the character array stream car.read(); }catch(IOException e) { // if any I/O error occurs System.out.print("Stream closed"); }finally { // releases all the system resources associated with this stream if(car!=null) car.close(); } } }[/c] Output Following is the result will be displayed. [c]Stream closed[/c] Usage of Long skip (long m) method. [c]import java.io.CharArrayReader; import java.io.IOException; public class CharArrayRead { public static void main(String[] args) { CharArrayReader car = null; char[] ch = {'S', 'P', 'L', 'E', 'S','S', 'O', 'N', 'S'}; try{ // creating a new character array reader car = new CharArrayReader(ch); int value = 0; // TO read till the end of the stream while((value=car.read())!=-1) { // For to convert integer to character char c = (char)value; // print the characters System.out.print(c + "; "); // For to skip single character. long l = car.skip(1); System.out.println("Characters Skipped : "+l); } }catch(IOException e) { // if any I/O error occurs e.printStackTrace(); }finally { // releases all the system resources associated with this stream if(car!=null) car.close(); } } }[/c] Output Following is the result will be displayed. [c] L; Characters Skipped : 1 S; Characters Skipped : 1 O; Characters Skipped : 1 S; Characters Skipped : 0 [/c]

Summary

shape Key Points

  • The Java.io CharArrayReader is the class used for reading of data in the form of character stream from the character array.
  • The character buffer is used internally as an input stream for Java.io CharArrayReader.
  • But it doesn't use any type of resources like connection or file.