Java - BufferedInputStream skip() method



Description

The Java BufferedInputStream skip(long) method skips n bytes of data from the buffered input stream. The number of bytes skipped id returned as long. For negative n, no bytes are skipped.

The skip method of BufferedInputStream creates a byte array which is read into until n bytes are read or the end of the stream is reached.

Declaration

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

public long skip(long n)

Parameters

n − number of bytes to be skipped.

Return Value

Returns actual number of bytes to be skipped.

Exception

IOException − If the stream not supporting seek, or if other I/O error occurs.

Assumption

Assuming we have a text file example.txt, which has the following content. This file will be used as an input for our example programs −

ABCDEFGHIJKLMNOPQRSTUVWXYZ

Example - Skip single byte

The following example shows the usage of Java BufferedInputStream skip() method.

BufferedInputStreamDemo.java

package com.tutorialspoint;

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

public class BufferedInputStreamDemo {
   public static void main(String[] args) throws Exception {
      InputStream is =null;
      BufferedInputStream bis = null;
      
      try {
         // open input stream test.txt for reading purpose.
         is = new FileInputStream("example.txt");			
         
         // input stream is converted to buffered input stream
         bis = new BufferedInputStream(is);
         
         // read until a single byte is available
         while(bis.available()>0) {
         
            // skip single byte from the stream
            bis.skip(1);
         
            // read next available byte and convert to char
            char c = (char)bis.read();
         
            // print character
            System.out.print(" " + c);
         }
      } catch (IOException e) {
         e.printStackTrace();
      } finally {
         // releases resources from the streams			
         if(is!=null)
            is.close();
         if(bis!=null)
            bis.close();
      }
   }
}

Output

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

 B D F H J L N P R T V X Z

Example - Skipping a Specific Number of Bytes

The following example shows the usage of Java BufferedInputStream skip() method.

BufferedInputStreamDemo.java

package com.tutorialspoint;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;

public class BufferedInputStreamDemo {
   public static void main(String[] args) {
      byte[] data = "Hello, this is a skip example!".getBytes();
      // open input stream test.txt for reading purpose.
      try (BufferedInputStream bis = new BufferedInputStream(
         new ByteArrayInputStream(data))) {
         System.out.println("Before skipping:");
         System.out.print((char) bis.read()); // Read and print the first byte (H)

         // Skip the next 7 bytes
         bis.skip(7);
         System.out.println("\nAfter skipping 7 bytes:");

         // Read and print the next byte (should be 't')
         System.out.print((char) bis.read());
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

Output

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

Before skipping:
H
After skipping 7 bytes:
h

Explanation

  • Data Source

    • A ByteArrayInputStream contains the string "Hello, this is a skip example!".

    • The stream is wrapped in a BufferedInputStream.

  • Using skip(long n)

    • The first byte (H) is read and printed.

    • The skip(7) method discards the next 7 bytes (ello, t), advancing the stream position.

  • Reading After Skip

    • After skipping, the next byte is read and printed (t).

Example - Skipping a Specific Number of Bytes

The following example shows the usage of Java BufferedInputStream skip() method.

BufferedInputStreamDemo.java

package com.tutorialspoint;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;

public class BufferedInputStreamDemo {
   public static void main(String[] args) {
      byte[] data = "Short data".getBytes();

      try (BufferedInputStream bis = new BufferedInputStream(
         new ByteArrayInputStream(data))) {
         // Skip 20 bytes (more than the stream length)
         long skippedBytes = bis.skip(20);

         System.out.println("Bytes attempted to skip: 20");
         System.out.println("Bytes actually skipped: " + skippedBytes);

         // Try reading after skipping
         int nextByte = bis.read();
         if (nextByte == -1) {
            System.out.println("No more data to read (end of stream).");
         } else {
            System.out.println("Next byte after skipping: " + (char) nextByte);
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

Output

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

Bytes attempted to skip: 20
Bytes actually skipped: 10
No more data to read (end of stream).

Explanation

  • Data Source

    • The ByteArrayInputStream contains "Short data", which is only 10 bytes long.

  • Using skip(long n)

    • The skip(20) method attempts to skip 20 bytes, but only skips the available 10 bytes.

    • The method returns the actual number of bytes skipped (10).

  • Reading After Skip

    • The stream reaches the end, and attempting to read further returns -1.

Key Points

  • Behavior

    • The skip(long n) method may not skip all requested bytes, especially if fewer bytes are available in the stream.

    • The method returns the actual number of bytes skipped.

  • Use Cases

    • Skipping over unimportant or irrelevant sections of the stream.

    • Efficiently moving to a specific point in the data.

  • Limitations

    • If the stream reaches the end during a skip, subsequent reads return -1.

java_io_bufferedinputstream.htm