Home »
Java programming language
Java InputStream skip() Method with Example
InputStream Class skip() method: Here, we are going to learn about the skip() method of InputStream Class with its syntax and example.
Submitted by Preeti Jain, on April 03, 2020
InputStream Class skip() method
- skip() method is available in java.io package.
- skip() method is used to skip the given number of bytes of data from this InputStream.
- skip() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
- skip() method may throw an exception at the time of skipping bytes data.
IOException: This exception may throw when getting any input/output error while performing.
Syntax:
public long skip(long number);
Parameter(s):
- long number – represents the number of bytes to be skipped.
Return value:
The return type of the method is long, it returns the exact number of bytes skipped.
Example:
// Java program to demonstrate the example
// of long skip(long number) method
// of InputStream
import java.io.*;
public class SkipOfIS {
public static void main(String[] args) throws Exception {
InputStream is_stm = null;
try {
// Instantiates FileInputStream
is_stm = new FileInputStream("D:\\includehelp.txt");
for (int val = 0; val < 8; ++val) {
// By using read() method is to read
// a byte from is_stm
is_stm.read();
// Display corresponding bytes value
byte b = (byte) val;
// Display value of b
System.out.println("is_stm.read(): " + b);
// By using skip() method is to skip
// 1 bytes data from the is_stm
long skip_byte = is_stm.skip(1);
System.out.println("is_stm.skip(1): " + skip_byte);
System.out.println();
}
} catch (Exception ex) {
System.out.println(ex.toString());
} finally {
// with the help of this block is to
// free all necessary resources linked
// with the stream
if (is_stm != null) {
is_stm.close();
}
}
}
}
Output
is_stm.read(): 0
is_stm.skip(1): 1
is_stm.read(): 1
is_stm.skip(1): 1
is_stm.read(): 2
is_stm.skip(1): 1
is_stm.read(): 3
is_stm.skip(1): 1
is_stm.read(): 4
is_stm.skip(1): 1
is_stm.read(): 5
is_stm.skip(1): 1
is_stm.read(): 6
is_stm.skip(1): 1
is_stm.read(): 7
is_stm.skip(1): 1