BufferedInputStream skip(long) method in Java with Examples

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,
  • Wen 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”.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
         }
    }
}

chevron_right


Output:

 E K

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
         }
    }
}

chevron_right


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 DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.