PUBLISHED ON: MAY 1, 2021
Java ByteArrayInputStream skip() Method
In this tutorial, we will learn about the skip(long x)
method of the ByteArrayInputStream class in Java. This method is used to skip the x bytes of input from the input stream. It is a non-static method, available in the java.io package, which should only be accessed using class objects. It will throw an exception if accessed through class name. But it will not throw an exception at the time of skipping bytes.
Syntax:
This is the syntax declaration of the skip(long x)
method. It accepts a single mandatory parameter x which specifies the number of bytes to be skipped and returns the number of skipped bytes.
public long skip(long x)
Example: ByteArrayInputStream skip() Method
In this example, we are implementing the skip() method to skip characters from the input stream, here in the current stream there are 5 elements. Firstly we read for 5 and then call the skip method with parameter 1 it means it will skip only one character and then again we read so in this example we skipped element 6 from the buffer while reading.
import java.io.ByteArrayInputStream;
import java.io.IOException;
public class StudyTonight
{
public static void main(String[] args) throws IOException
{
try
{
byte[] buf = { 5, 6, 7, 8, 9 };
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(buf);
System.out.println(byteArrayInputStream.read());
byteArrayInputStream.skip(1);
System.out.println(byteArrayInputStream.read());
System.out.println(byteArrayInputStream.read());
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
5
7
8
Example: ByteArrayInputStream skip() Method
This example is the same as the example given in above but this time we skip the first 3 characters so it will not read the first three characters and start reading from the fourth character.
import java.io.ByteArrayInputStream;
import java.io.IOException;
public class StudyTonight
{
public static void main(String[] args) throws IOException
{
try
{
byte[] buf = { 5, 6, 7, 8, 9 };
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(buf);
byteArrayInputStream.skip(3);
System.out.println(byteArrayInputStream.read());
System.out.println(byteArrayInputStream.read());
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
8
9
Conclusion:
In this tutorial, we learned about skip(long x)
method of ByteArrayInputStream class in Java. The skip(long x)
of ByteArrayInputStream class in Java is used to skip the x bytes of input from the input stream. It accepts a single parameter x which specifies the number of bytes to be skipped and returns the number of skipped bytes.