Java - InputStream skip(long n) method



Description

The Java InputStream skip(long n) method skips over and discards n bytes from the input stream. This is useful when you want to ignore a portion of the data while reading. Returns the actual number of bytes skipped (could be less than n if EOF is reached). If n is negative, it does not skip any bytes.

Declaration

Following is the declaration for java.io.InputStream.skip(long n) method −

public long skip(long n)

Parameters

n − The number of bytes to be skipped.

Return Value

The method does not return any value.

Exception

  • IOException − If an I/O error occurs, or if the stream does not support seek.

Example - Usage of InputStream skip(long n) method

The following example shows the usage of Java InputStream skip(long n) method.

InputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.InputStream;

public class InputStreamDemo {
   public static void main(String[] args) throws Exception {
      InputStream is = null;
      int i;
      char c;
      
      try {
         // new input stream created
         is = new FileInputStream("test.txt");
         
         while((i = is.read())!=-1) {
         
            // converts int to char
            c = (char)i;
            
            // prints character
            System.out.println("Character Read: "+c);
            
            // skip one byte
            is.skip(1);
         }
         
      } catch(Exception e) {
         // if any I/O error occurs
         e.printStackTrace();
      } finally {
         // releases system resources associated with this stream
         if(is!=null)
            is.close();
      }
   }
}

Output(Assuming test.txt contains "ABCDE")

Let us compile and run the above program, this will produce the following result−

Character Read: A
Character Read: C
Character Read: E

Example - Skipping Bytes in a FileInputStream

The following example shows the usage of Java InputStream skip(long n) method.

InputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class InputStreamDemo {
   public static void main(String[] args) {
      try (InputStream inputStream = new FileInputStream("example.txt")) {
         System.out.println("Skipping 5 bytes...");
         long skippedBytes = inputStream.skip(5); // Skip first 5 bytes

         System.out.println("Bytes actually skipped: " + skippedBytes);

         // Reading and printing next character after skipping
         int data = inputStream.read();
         System.out.println("Character read after skipping: " + (char) data);

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(if example.txt contains "HelloWorld")

Let us compile and run the above program, this will produce the following result−

Skipping 5 bytes...
Bytes actually skipped: 5
Character read after skipping: W

Explanation

  • Uses FileInputStream to read from "example.txt".

  • Calls skip(5) to skip the first 5 bytes.

  • Reads the next character after skipping.

Example - Handling EOF While Skipping in BufferedInputStream

The following example shows the usage of Java InputStream skip(long n) method.

InputStreamDemo.java

package com.tutorialspoint;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class InputStreamDemo {
   public static void main(String[] args) {
      try (InputStream inputStream = new BufferedInputStream(new FileInputStream("example.txt"))) {
         long skippedBytes = inputStream.skip(1000); // Attempting to skip more than file size
         System.out.println("Attempted to skip 1000 bytes, actually skipped: " + skippedBytes);

         // Checking if any data is left to read
         int data = inputStream.read();
         if (data == -1) {
            System.out.println("Reached end of file.");
         } else {
            System.out.println("Character read after skipping: " + (char) data);
         }

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(if example.txt contains "Microservices" but is less than 1000 bytes)

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

  • Uses BufferedInputStream, which supports efficient skipping.

  • Tries to skip 1000 bytes, even if the file is smaller.

  • The skip() method skips the required bytes.

  • Checks if more data is available using read().

java_io_inputstream.htm
Advertisements