Skip to content
Related Articles

Related Articles

Improve Article

BufferedInputStream skip(long) method in Java with Examples

  • Last Updated : 21 Apr, 2021
Geek Week

The skip(long) method of BufferedInputStream class in Java is used to skip n bytes of data from the buffered input stream. The number of bytes skipped is stored and returned as long type. The termination condition involves either of the two: 
 

  • Either reading into a byte array until n-bytes are covered or,
  • When the end of the input, stream is met.

However, if a negative value is passed then no skipping takes place.
Syntax: 
 

public long skip(long n)

Parameters: This method accepts n of long type that represents the number of bytes required to be skipped from the input stream.
Return value: This method returns the number of bytes skipped as long type.
Exception: This method throws IOException if this input stream has been closed by invoking the close() method or the stream is unsupportive of seeking, or any other I/O error occurs.
Below programs illustrates skip(long) method in BufferedInputStream class in IO package:
Program 1: Assume the existence of file “c:/demo.txt”. 
 

Java




// Java program to illustrate
// BufferedInputStream skip(long) method
import java.io.*;
public class GFG {
    public static void main(String[] args)
    {
    
        // Create input stream 'demo.txt'
        // for reading containing text "GEEK"
        FileInputStream inputStream = 
        new FileInputStream("c:/demo.txt");
    
        // Convert inputStream to 
        // bufferedInputStream
        BufferedInputStream buffInputStr 
              = new BufferedInputStream(
                          inputStream);
          
        // Read until a single
        // byte is available
        while(buffInputStr.available()>0) {
          
            // Skip single byte from the stream
            buffInputStr.skip(1);
          
            // Read next available byte and
            // convert to char
            char c = (char) buffInputStr.read();
          
            // Print character
            System.out.print(" " + c);
         }
    }
}
Output: 
 E K

 

Program 2: Assume the existence of file “c:/demo.txt”. 
 



Java




// Java program to illustrate
// BufferedInputStream skip(long) method
import java.io.*;
public class GFG {
    public static void main(String[] args)
    {
    
        // Create input stream 'demo.txt'
        // for reading containing text "GEEKSFORGEEKS"
        FileInputStream inputStream = 
        new FileInputStream("c:/demo.txt");
    
        // convert inputStream to 
        // bufferedInputStream
        BufferedInputStream buffInputStr
            = new BufferedInputStream(
                        inputStream);
          
        // Read until a single
        // byte is available
        while(buffInputStr.available()>0) {
          
            // Skip single byte from the stream
            buffInputStr.skip(3);
          
            // Read next available byte and
            // convert to char
            char c = (char) buffInputStr.read();
          
            // Print character
            System.out.print(" " + c);
         }
    }
}
Output: 
K R K

 

References: 
https://docs.oracle.com/javase/10/docs/api/java/io/BufferedInputStream.html#skip(long)
 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :