Java - CharArrayReader skip(long n) method



Description

The Java CharArrayReader skip(long n) method skips the specified number of characters from the current reading position. If n is greater than the remaining characters, it skips only up to the end of the stream.

Declaration

Following is the declaration for java.io.CharArrayReader.skip(long n) method −

public long skip(long n)

Parameters

n− The number of characters to be skipped.

Return Value

The number of characters actually skipped.

Exception

IOException − If any I/O error occurs or the stream is closed.

Example - Using CharArrayReader skip() method

The following example shows the usage of Java CharArrayReader skip() method.

CharArrayReaderDemo.java

package com.tutorialspoint;

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

public class CharArrayReaderDemo {
   public static void main(String[] args) {      
      CharArrayReader car = null;
      char[] ch = {'A', 'B', 'C', 'D', 'E'};
      
      try {
         // create new character array reader
         car = new CharArrayReader(ch);
         
         int value = 0;
         
         // read till the end of the stream
         while((value = car.read())!=-1) {
         
            // convert integer to char
            char c = (char)value;
            
            // print characters
            System.out.print(c + "; ");
            
            // skip single character
            long l = car.skip(1);
            System.out.println("Characters Skipped : "+l);
         }
         
      } catch(IOException e) {
         // if I/O error occurs
         e.printStackTrace();
      } finally {
         // releases any system resources associated with the stream
         if(car!=null)
            car.close();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

A; Characters Skipped : 1
C; Characters Skipped : 1
E; Characters Skipped : 0

Example - Skipping the First Few Characters

The following example shows the usage of Java CharArrayReader skip() method.

CharArrayReaderDemo.java

package com.tutorialspoint;

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

public class CharArrayReaderDemo {
   public static void main(String[] args) {
      char[] data = "Java Programming Language".toCharArray();

      try (CharArrayReader reader = new CharArrayReader(data)) {
         System.out.println("Initial content: " + new String(data));

         // Skip first 5 characters ("Java ")
         reader.skip(5);
         System.out.println("After skipping 5 characters, reading the rest:");

         // Read and print remaining characters
         int charData;
         while ((charData = reader.read()) != -1) {
            System.out.print((char) charData);
         }

      } catch (IOException e) {
         System.err.println("IOException occurred: " + e.getMessage());
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Initial content: Java Programming Language
After skipping 5 characters, reading the rest:
Programming Language

Explanation

  • Initialize CharArrayReader with "Java Programming Language".

  • Call skip(5) to skip the first 5 characters ("Java ").

  • Read and print remaining characters ("Programming Language").

  • If skip(n) exceeds available characters, it stops at the end of the stream.

Example - Skipping More Characters Than Available

The following example shows the usage of Java CharArrayReader reset() method.

CharArrayReaderDemo.java

package com.tutorialspoint;

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

public class CharArrayReaderDemo {
   public static void main(String[] args) {
      char[] data = "Short Text".toCharArray();

      try (CharArrayReader reader = new CharArrayReader(data)) {
         System.out.println("Initial content: " + new String(data));

         // Try to skip more characters than available
         long skipped = reader.skip(50);
         System.out.println("Characters skipped: " + skipped);

         // Check if we reached the end
         if (reader.read() == -1) {
            System.out.println("Reached the end of the stream after skipping.");
         }

      } catch (IOException e) {
         System.err.println("IOException occurred: " + e.getMessage());
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Initial content: Short Text
Characters skipped: 10
Reached the end of the stream after skipping.

Explanation

  • Initialize CharArrayReader with "Short Text".

  • Call skip(50), which is greater than the available 10 characters.

  • skip() only skips the remaining characters (10 in this case) and returns 10, not 50.

  • Reading after skip returns -1, indicating the end of the stream.

Key Takeaways About skip(long n)

  • Skips n characters from the current position.

  • If n exceeds remaining characters, it stops at the end.

  • Does not throw an error when skipping beyond available datait just returns the actual number of skipped characters.

  • Efficient for skipping unwanted data before processing relevant content.

These examples show how skip() helps ignore parts of a character stream efficiently!

java_io_chararrayreader.htm
Advertisements