Java FileInputStream skip() Method
Example
Skip the first 6 bytes of a file, then read the next byte:
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
FileWriter myWriter = new FileWriter("filename.txt");
myWriter.write("Hello World");
myWriter.close();
FileInputStream input = new FileInputStream("filename.txt");
System.out.println(input.skip(6));
System.out.println((char) input.read());
input.close();
}
}
Definition and Usage
The skip() method jumps over a number of bytes in the file. The skipped bytes are not read.
It returns the number of bytes that were skipped. In the example, the first 6 bytes (Hello and a space) are skipped, so the next byte is W.
Syntax
public long skip(long n)
Parameter Values
| Parameter | Description |
|---|---|
| n | Required. The number of bytes to skip. |
Technical Details
| Returns: | The number of bytes that were skipped, as a long. |
|---|---|
| Throws: | IOException - if the stream is closed, or if the bytes cannot be skipped. |
Related Pages
❮ FileInputStream Methods