PushbackInputStream skip() method in Java with Examples

The skip(long n) method of PushbackInputStream class in Java is used to skip over and discards n bytes of data from this input stream. This method first skips over the bytes in the pushback buffer, and then calls the skip method of the main input stream. It returns the actual number of bytes skipped.
Syntax:
public long skip(long n)
throws IOException
Overrides: This method overrides the skip() method of FilterInputStream class.
Parameters: This method accepts one parameter n which represents the number of bytes to be skipped.
Return value: This method returns the actual number of bytes skipped.
Exceptions: This method throws IOException if the stream has been closed by calling close() method or if an I/O error occurs.
Below programs illustrate skip(long) method of PushbackInputStream class in IO package:
Program 1:
// Java program to illustrate// PushbackInputStream skip(long) method  import java.io.*;  public class GFG {    public static void main(String[] args)        throws IOException    {          // Create an array        byte[] byteArray            = new byte[] { 'G', 'E', 'E',                           'K', 'S' };          // Create inputStream        InputStream inputStr            = new ByteArrayInputStream(byteArray);          // Create object of        // PushbackInputStream        PushbackInputStream pushbackInputStr            = new PushbackInputStream(inputStr);          // Revoke skip()        pushbackInputStr.skip(2);          for (int i = 0; i < byteArray.length - 2; i++) {            // Read the character            System.out.print(                (char)pushbackInputStr.read());        }    }} |
EKS
Program 2:
// Java program to illustrate// PushbackInputStream skip(long) method  import java.io.*;  public class GFG {    public static void main(String[] args)        throws IOException    {          // Create an array        byte[] byteArray            = new byte[] { 'G', 'E', 'E', 'K', 'S',                           'F', 'O', 'R', 'G', 'E',                           'E', 'K', 'S' };          // Create inputStream        InputStream inputStr            = new ByteArrayInputStream(byteArray);          // Create object of        // PushbackInputStream        PushbackInputStream pushbackInputStr            = new PushbackInputStream(inputStr);          // Revoke skip()        pushbackInputStr.skip(8);          for (int i = 0; i < byteArray.length - 8; i++) {            // Read the character            System.out.print(                (char)pushbackInputStr.read());        }    }} |
GEEKS
References:
https://docs.oracle.com/javase/10/docs/api/java/io/PushbackInputStream.html#skip(long)



