Home » Java programming language

Java CharArrayReader skip() Method with Example

CharArrayReader Class skip() method: Here, we are going to learn about the skip() method of CharArrayReader Class with its syntax and example.
Submitted by Preeti Jain, on March 27, 2020

CharArrayReader Class skip() method

  • skip() method is available in java.io package.
  • skip() method is used to skip the given number of characters from this CharArrayReader.
  • skip() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • skip() method may throw an exception at the time of skipping characters.
    IOException: This exception may throw while getting any input/output error.

Syntax:

    public  long skip(long number);

Parameter(s):

  • long number – represents the number of characters to be skipped.

Return value:

The return type of the method is long, it returns the exact number of characters to be skipped.

Example:

// Java program to demonstrate the example 
// of long skip(long number) method of 
// CharArrayReader

import java.io.*;

public class SkipOfCAR {
 public static void main(String[] args) {
  CharArrayReader car_stm = null;
  char[] c_arr = {
   'a',
   'b',
   'c',
   'd'
  };

  try {
   // Instantiates CharArrayReader
   car_stm = new CharArrayReader(c_arr);
   int val = 0;

   // By using skip() methos is to skip
   // the number of characters
   long skip = car_stm.skip(2);
   System.out.println("car_stm.skip(2): " + skip);

   System.out.println("After skip(): ");
   // Read after skipping
   while ((val = car_stm.read()) != -1) {
    char ch = (char) val;
    System.out.print(ch + " ");
   }
  } catch (Exception ex) {
   System.out.print(ex.toString());
  } finally {

   // Free all system resources linked
   // with the stream after closing
   // the stream

   if (car_stm != null)
    car_stm.close();
  }
 }
}

Output

car_stm.skip(2): 2
After skip(): 
c d 


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.