Friday, October 10, 2025
HomeLanguagesJavaByteArrayInputStream skip() method in Java with Examples

ByteArrayInputStream skip() method in Java with Examples

The skip() method is a built-in method of the Java.io.ByteArrayInputStream which skips arg bytes in the input stream. A fewer bytes is skipped if the stream has reached towards the end.

Syntax:

public long skip(long args)

Parameters: The function accepts a single mandatory parameter args which specifies the number of bytes to be skipped

Return Value: The function returns the number of skip bytes.

Errors an Exceptions: The function throws an IOException when I/O error occurs.

Below is the implementation of the above function:

Program 1:




// Java program to implement
// the above function
import java.io.*;
  
public class Main {
    public static void main(String[] args) throws Exception
    {
  
        byte[] buf = { 5, 6, 7, 8, 9 };
  
        // Create new byte array input stream
        ByteArrayInputStream exam
            = new ByteArrayInputStream(buf);
  
        // print bytes
        System.out.println(exam.read());
  
        // Skips 1 element
        exam.skip(1);
  
        System.out.println(exam.read());
        System.out.println(exam.read());
    }
}


Output:

5
7
8

Program 2:




// Java program to implement
// the above function
import java.io.*;
  
public class Main {
    public static void main(String[] args) throws Exception
    {
  
        byte[] buf = { 1, 2, 3, 4, 5, 6, 7, 8 };
  
        // Create new byte array input stream
        ByteArrayInputStream exam
            = new ByteArrayInputStream(buf);
  
        // print bytes
        System.out.println(exam.read());
  
        // Skips 3 elements
        exam.skip(3);
  
        System.out.println(exam.read());
        System.out.println(exam.read());
    }
}


Output:

1
5
6

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

RELATED ARTICLES

Most Popular

Dominic
32349 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11878 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6837 POSTS0 COMMENTS
Ted Musemwa
7097 POSTS0 COMMENTS
Thapelo Manthata
6792 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS