Java - FilterReader skip(long n) method



Description

The Java FilterReader skip(long n) method skips over and discards n characters from the input stream. This is useful when you want to ignore a portion of the data while reading. Returns the actual number of characters skipped (could be less than n if EOF is reached). If n is negative, it does not skip any characters.

Declaration

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

public void skip(long n)

Parameters

n − Skips n number of characters from the stream.

Return Value

The method returns number of characters actually skipped.

Exception

IOException − If an I/O error occurs.

Example - Usage of FilterReader skip(long n) method

The following example shows the usage of Java FilterReader skip(long n) method.

FilterReaderDemo.java

package com.tutorialspoint;

import java.io.FilterReader;
import java.io.Reader;
import java.io.StringReader;

public class FilterReaderDemo {
   public static void main(String[] args) throws Exception {
      FilterReader fr = null;
      Reader r = null;
      int i = 0;
      long l = 0l;
      char c;
      
      try {
         // create new reader
         r = new StringReader("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
          
         // create new filter reader
         fr = new FilterReader(r) {
         };
         
         // read till the end of the filter reader
         while((i = fr.read())!=-1) {
         
            // convert integer to character
            c = (char)i;
            
            // prints
            System.out.println("Character read: "+c);
            
            // number of characters actually skipped
            l = fr.skip(2);
            
            // prints
            System.out.println("Character skipped: "+l);
         }
         
      } catch(Exception e) {
         // if any I/O error occurs
         e.printStackTrace();
      } finally {
         // releases system resources associated with this stream
         if(r!=null)
            r.close();
         if(fr!=null)
            fr.close();
      }
   }
}

Output

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

Character read: A
Character skipped: 2
Character read: D
Character skipped: 2
Character read: G
Character skipped: 2
Character read: J
Character skipped: 2
Character read: M
Character skipped: 2
Character read: P
Character skipped: 2
Character read: S
Character skipped: 2
Character read: V
Character skipped: 2
Character read: Y
Character skipped: 1

Example - Skipping Characters in a BufferedReader

The following example shows the usage of Java FilterReader skip(long n) method.

FilterReaderDemo.java

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class FilterReaderDemo {
   public static void main(String[] args) {
      try (BufferedReader fr = new BufferedReader(new FileReader("example.txt"))) {
         System.out.println("Skipping 5 characters...");
         long skippedChars = fr.skip(5); // Skip first 5 characters

         System.out.println("Characters actually skipped: " + skippedChars);

         // Reading and printing next character after skipping
         int data = fr.read();
         System.out.println("Character read after skipping: " + (char) data);

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(if example.txt contains "HelloWorld")

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

Skipping 5 characters...
Characters actually skipped: 5
Character read after skipping: W

Explanation

  • Uses BufferedReader, which is a FilterReader subclass.

  • Calls skip(5) to skip the first 5 characters.

  • Reads the next character after skipping.

Example - Handling EOF While Skipping in PushbackReader

The following example shows the usage of Java FilterReader skip(long n) method.

FilterReaderDemo.java

package com.tutorialspoint;

import java.io.FileReader;
import java.io.FilterReader;
import java.io.IOException;
import java.io.PushbackReader;

public class FilterReaderDemo {
   public static void main(String[] args) {
      try (FilterReader fr = new PushbackReader(new FileReader("example.txt"))) {
         long skippedChars = fr.skip(1000); // Attempting to skip more than file size
         System.out.println("Attempted to skip 1000 characters, actually skipped: " + skippedChars);

         // Checking if any data is left to read
         int data = fr.read();
         if (data == -1) {
            System.out.println("Reached end of file.");
         } else {
            System.out.println("Character read after skipping: " + (char) data);
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output (if example.txt contains "Microservices" but is less than 1000 characters)

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

Attempted to skip 1000 characters, actually skipped: 13
Reached end of file.

Explanation

  • Uses PushbackReader, which supports skip().

  • Tries to skip 1000 characters, even if the file is smaller.

  • The skip() method skips only available characters.

  • Checks if more data is available using read().

java_io_filterreader.htm
Advertisements