Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java FilterInputStream skip() Method

In this tutorial, we will learn about the skip() method of the FilterInputStream class in Java. This method is used to skip over and discard the given number of bytes from the current Filter Input Stream. It is a non-static method and available in the java.io package.

Syntax

This is the syntax declaration of this method. It takes 'n' number of bytes to be skipped over as the parameter and returns the total number of bytes skipped.

public long skip(long n) throws IOException

Example: Skip Bytes in FilterInput Stream in Java

In this example, we are going to use skip() method to discard specified number of bytes from the filter input stream in Java. See the example below.

import java.io.*;

public class studytonight {
 public static void main(String[] args) throws Exception {
  FileInputStream st = null;
  FilterInputStream std = null;
  int count = 0;

  try {
   
   st = new FileInputStream("E:\studytonight\output.txt");
   std = new BufferedInputStream(st);

   
   while ((count = st.read()) != -1) {

   
    char ch = (char) count;

    
    System.out.println("ch: " + ch);    

    long skip = st.skip(2);
    System.out.println("st.skip(2): " + skip);
   }
  } catch (Exception ex) {
   System.out.println(ex.toString());

  } finally {
   

   if (std != null) {
    st.close();

    if (st != null) {
     st.close();
    }
   }
  }
 }
}


ch: S
st.skip(2): 2
ch: a
st.skip(2): 2
ch: r
st.skip(2): 2
ch: .
st.skip(2): 2
ch: .
st.skip(2): 2

Conclusion

In this tutorial, we learned about the skip() method of the FilterInputStream class in Java, which skips the given 'n' number of bytes from the current filter input stream and returns the exact number of bytes skipped. 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.