
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - FileInputStream skip(long n) method
Description
The Java FileInputStream skip(long n) method is used to skip over and discard n bytes of data from the input stream. This method is helpful when you want to move the file pointer forward by a specific number of bytes before reading data.
If n is greater than the number of remaining bytes in the file, it skips only the available bytes.
If n is negative, it does not skip any bytes.
The method returns the actual number of bytes skipped, which may be less than the requested number if the end of the file is reached.
Declaration
Following is the declaration for java.io.FileInputStream.skip(long n) method −
public int skip(long n)
Parameters
n − The number of bytes to be skipped.
Return Value
The method returns the actual number of bytes skipped.
Exception
IOException − If an I/O error occurs, if n is negative or if the stream does not support seek.
Example - Usage of FileInputStream skip(long n) method
The following example shows the usage of Java FileInputStream skip(long n) method.
FileInputStreamDemo.java
package com.tutorialspoint; import java.io.IOException; import java.io.FileInputStream; public class FileInputStreamDemo { public static void main(String[] args) throws IOException { FileInputStream fis = null; int i = 0; char c; try { // create new file input stream fis = new FileInputStream("test.txt"); // skip bytes from file input stream fis.skip(4); // read bytes from this stream i = fis.read(); // converts integer to character c = (char)i; // prints System.out.print("Character read: "+c); } catch(Exception ex) { // if any error occurs ex.printStackTrace(); } finally { // releases all system resources from the streams if(fis!=null) fis.close(); } } }
Output
Assumption
Assuming we have a text file test.txt in current directory, which has the following content. This file will be used as an input for our example program.
ABCDEF
Let us compile and run the above program, this will produce the following result−
Character read: E
Example - Skipping a Specific Number of Bytes
The following example shows the usage of Java FileInputStream skip(long n) method. This example reads data from a file using read(byte[] buf, int off, int len) method in chunks and prints it as text.
FileInputStreamDemo.java
package com.tutorialspoint; import java.io.FileInputStream; import java.io.IOException; public class FileInputStreamDemo { public static void main(String[] args) { try (FileInputStream fis = new FileInputStream("example.txt")) { System.out.println("Available bytes before skipping: " + fis.available()); // Skipping 5 bytes long skippedBytes = fis.skip(5); System.out.println("Skipped Bytes: " + skippedBytes); // Reading the next byte after skipping int data = fis.read(); System.out.println("Byte read after skipping: " + (char) data); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Available bytes before skipping: 14 Skipped Bytes: 5 Byte read after skipping: ,
Explanation
The program opens example.txt using FileInputStream.
It prints the available bytes before skipping.
It attempts to skip 5 bytes.
It then reads and prints the next character after skipping.
Example - Handling End of File (EOF)
The following example shows the usage of Java FileInputStream skip(long n) method. This example reads part of the file into a specific portion of an array.
FileInputStreamDemo.java
package com.tutorialspoint; import java.io.FileInputStream; import java.io.IOException; public class FileInputStreamDemo { public static void main(String[] args) { try (FileInputStream fis = new FileInputStream("example.txt")) { // Trying to skip more bytes than the file contains long skippedBytes = fis.skip(1000); System.out.println("Attempted to skip 1000 bytes, actually skipped: " + skippedBytes); // Checking if more data is available int data = fis.read(); if (data == -1) { System.out.println("Reached end of file."); } else { System.out.println("Byte read after skipping: " + (char) data); } } catch (IOException e) { e.printStackTrace(); } } }
Output(contents of example.txt)
Let us compile and run the above program, this will produce the following result−
Attempted to skip 1000 bytes, actually skipped: 1000 Reached end of file.
Explanation
This example attempts to skip 1000 bytes, but if the file has fewer bytes, it will skip only the available ones.
If there's no data left, read() returns -1, indicating the end of the file.
This demonstrates how skip(n) handles cases where n is larger than the file size.