Java CharArrayReader skip() Example

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


Syntax is : public long skip(long n) throws IOException
Method skips characters. Returns the number of characters that were skipped. The n parameter may be negative, even though the skip method of the Reader superclass throws an exception in this case. If n is negative, then this method does nothing and returns 0.

Here is the code.
/**
 * @(#) SkipCharArrayReader.java
 * A class representing use of method skip() of CharArrayReader 
 class in java.io Package.
 * @Version  31-May-2008
 @author   Rose India Team
 */
import java.io.*;
public class SkipCharArrayReader {
     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);
     System.out.println("\nFirst character in buffer is : "+(char)obj.read());
       System.out.println("Next character in buffer is  : "+(char)obj.read());

     // skip() method call.
         obj.skip(2);

     System.out.println("2 elements skipped successfuly!");
         System.out.println("Next element in buffer is   : "+(char)obj.read());
    }
}
/* 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
First character in buffer is : M
Next character in buffer is  : A
2 elements skipped successfuly!
Next element in buffer is   : N

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

HOME | COPYRIGHT | CONTACT US