Java CharArrayReader read() Example

CharArrayReader class read() method example. This example shows you how to use read() method.


Syntax is : public int read(char[] b, int off, int len) throws IOException
This method reads characters into a portion of an array.

Here is the code
/**
 * @(#) Read1CharArrayReader.java
 * A class representing use of method read() of CharArrayReader 
 class in java.io Package.
 * @Version  31-May-2008
 @author   Rose India Team
 */
import java.io.*;
public class Read1CharArrayReader {
  public static void main(String[] argsthrows IOException{
         
    char[] buffer = {'M','A','H','E','N','D','R','A'};
     System.out.print("given character buffer is : ");
     for (int i=0;i<buffer.length ;i++ ){
       System.out.print(buffer[i]);
     }
    // Create object of CharArrayReader class
        CharArrayReader obj = new CharArrayReader(buffer);
    // read() method call.
      int charCount = obj.read(buffer, 04);
      System.out.println("\nnumber of characters read is  : " + charCount);
    }
}
/* Another constructor of class CharArrayReader:-
    public CharArrayReader(char[] buf, int offset, int length)
  Description:
    Creates a CharArrayReader from the specified array of chars.
    The resulting reader will start reading at the given offset. The total 
  number of char values that can be read from this reader will be either 
  length or buf.length-offset, whichever is smaller.

  Parameters:
        buf - Input buffer (not copied)
        offset - Offset of the first char to read
        length - Number of chars to read 
  Throws:
        IllegalArgumentException - If offset is negative or greater than 
    buf.length, or if length is negative, or if the sum of these two values
    is negative. */ 

Output of the program.
given character buffer is : MAHENDRA
number of characters read is  : 4

Post Comment
Name:
E-mail:
Contact no :
Comments:
  Refresh Image
Verify Image:
 
 
Your Comment's
 
 
 

HOME | COPYRIGHT | CONTACT US