BufferedInputStream skip(long) method in Java with Examples
Last Updated :
21 Apr, 2021
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,
- When 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”.
Java
import java.io.*;
public class GFG {
public static void main(String[] args)
{
FileInputStream inputStream =
new FileInputStream( "c:/demo.txt" );
BufferedInputStream buffInputStr
= new BufferedInputStream(
inputStream);
while (buffInputStr.available()> 0 ) {
buffInputStr.skip( 1 );
char c = ( char ) buffInputStr.read();
System.out.print( " " + c);
}
}
}
|
Program 2: Assume the existence of file “c:/demo.txt”.
Java
import java.io.*;
public class GFG {
public static void main(String[] args)
{
FileInputStream inputStream =
new FileInputStream( "c:/demo.txt" );
BufferedInputStream buffInputStr
= new BufferedInputStream(
inputStream);
while (buffInputStr.available()> 0 ) {
buffInputStr.skip( 3 );
char c = ( char ) buffInputStr.read();
System.out.print( " " + c);
}
}
}
|
References:
https://docs.oracle.com/javase/10/docs/api/java/io/BufferedInputStream.html#skip(long)
Share your thoughts in the comments
Please Login to comment...