Java - FilterInputStream skip(long n) method



Description

The Java FilterInputStream skip(long n) method skips over and discards n bytes of data from the input stream. This is useful when you want to ignore a portion of the data while reading. It returns the actual number of bytes skipped (could be less than n if EOF is reached). Does not read the skipped bytes, just moves the cursor forward. If n is negative, it does not skip any bytes.

Declaration

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

public void skip(long n)

Parameters

n − number of bytes to be skipped over.

Return Value

The method returns the number of bytes actually skipped.

Exception

  • IOException − If any I/O error occurs or the stream doesnot support seek.

Example - Usage of FilterInputStream skip(long n) method

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

FilterInputStreamDemo.java

package com.tutorialspoint;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

public class FilterInputStreamDemo {
   public static void main(String[] args) throws Exception {
      InputStream is = null; 
      FilterInputStream fis = null;
      int i = 0;
      char c;
      
      try {
         // create input streams
         is = new FileInputStream("test.txt");
         fis = new BufferedInputStream(is);
         
         while((i = fis.read())!=-1) {
         
            // converts integer to character
            c = (char)i;
            
            // skips 3 bytes
            fis.skip(3);
            
            // print
            System.out.println("Character read: "+c);
         }
         
      } catch(IOException e) {
         // if any I/O error occurs
         e.printStackTrace();
      } finally {
         // releases any system resources associated with the stream
         if(is!=null)
            is.close();
         if(fis!=null)
            fis.close();
      }
   }
}

Output(assuming test.txt contains ABCDEF)

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

Character read: A
Character read: E

Example - Skipping Bytes in a BufferedInputStream

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

FilterInputStreamDemo.java

package com.tutorialspoint;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;

public class FilterInputStreamDemo {
   public static void main(String[] args) {
      try (FilterInputStream fis = new BufferedInputStream(new FileInputStream("example.txt"))) {
         System.out.println("Skipping 5 bytes...");
         long skippedBytes = fis.skip(5); // Skip first 5 bytes

         System.out.println("Bytes actually skipped: " + skippedBytes);

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

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

Output(assuming example.txt contains HelloWorld)

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

Skipping 5 bytes...
Bytes actually skipped: 5
Byte read after skipping: W

Explanation

  • Uses BufferedInputStream, which is a FilterInputStream subclass.

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

  • Reads the next character after the skipped bytes.

Example - Handling EOF While Skipping in PushbackInputStream

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

FilterInputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.PushbackInputStream;

public class FileInputStreamDemo {
   public static void main(String[] args) {
      try (FilterInputStream fis = new PushbackInputStream(new FileInputStream("example.txt"))) {
         long skippedBytes = fis.skip(1000); // Attempting to skip more than file size
         System.out.println("Attempted to skip 1000 bytes, actually skipped: " + skippedBytes);

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

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

Output(assuming example.txt contains Microservices but is less than 1000 bytes)

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

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

Explanation

  • Uses PushbackInputStream, which supports skip().

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

  • The skip() method skips only available bytes.

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

java_io_filterinputstream.htm
Advertisements