Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Java - ObjectInputStream skipBytes(int len) method



Description

The Java ObjectInputStream skipBytes(int len) method skips bytes.

  • skipBytes(int len) only works with primitive types (e.g., int, float).

  • It does NOT work for skipping entire serialized objects like String, ArrayList, or Custom Objects.

  • For variable-length data like String, calculate the number of bytes to skip manually.

Declaration

Following is the declaration for java.io.ObjectInputStream.skipBytes(int len) method.

public int skipBytes(int len)

Parameters

len − The number of bytes to be skipped.

Return Value

This method returns the actual number of bytes skipped.

Exception

  • IOException − Any exception thrown by the underlying InputStream.

Example - Usage of ObjectInputStream skipBytes(int len) method

The following example shows the usage of Java ObjectInputStream skipBytes(int len) method.

ObjectInputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectInputStreamDemo {
   public static void main(String[] args) {
      String s = "Hello World!";
      
      try {
         // create a new file with an ObjectOutputStream
         FileOutputStream out = new FileOutputStream("test.txt");
         ObjectOutputStream oout = new ObjectOutputStream(out);

         // write something in the file
         oout.writeUTF(s);
         oout.writeUTF("This is an example");
         oout.flush();

         // create an ObjectInputStream for the file we created before
         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

         // skip 4 bytes and read the rest
         ois.skipBytes(4);
         for (int i = 0; i < ois.available() - 4; i++) {
            System.out.print("" + (char) ois.readByte());
         }
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Output

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

llo World!

Example - Skipping a Float Value Between Two Integers

The following example shows the usage of Java ObjectInputStream skipBytes(int len) method.

ObjectInputStreamDemo.java

package com.tutorialspoint;

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectInputStreamDemo {
   public static void main(String[] args) {
      String filename = "data_fixed.bin";

      // Step 1: Write an Integer, a Float, and another Integer to the file
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
         oos.writeInt(100);     // 4 bytes
         oos.writeFloat(3.14f); // 4 bytes
         oos.writeInt(200);     // 4 bytes
         System.out.println("Data written to file.");
      } catch (IOException e) {
         e.printStackTrace();
      }

      // Step 2: Read and skip the float value
      try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
         int firstInt = ois.readInt(); // Reads 100
         int skippedBytes = ois.skipBytes(4); // Skips the float value (4 bytes)

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

         if (skippedBytes == 4) { // Ensure skip was successful
            int secondInt = ois.readInt(); // Reads 200
            System.out.println("Second int (after skipping float): " + secondInt);
         } else {
            System.out.println("Not enough bytes to skip.");
         }
      } catch (EOFException e) {
         System.out.println("Reached the end of the file unexpectedly.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Data written to file.
First int: 100
Bytes actually skipped: 4
Second int (after skipping float): 200

Explanation

  • Writes an integer, a float, and another integer to a file.

  • Uses skipBytes(4) to skip over the float value while reading.

Example - Skipping multiple bytes

The following example shows the usage of Java ObjectInputStream skipBytes(int len) method.

ObjectInputStreamDemo.java

package com.tutorialspoint;

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectInputStreamDemo {
   public static void main(String[] args) {
      String filename = "data.bin";

      // Step 1: Write multiple integers to a file
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
         oos.writeInt(10);  // 4 bytes
         oos.writeInt(20);  // 4 bytes
         oos.writeInt(30);  // 4 bytes
         oos.writeInt(40);  // 4 bytes
         System.out.println("Objects written to file.");
      } catch (IOException e) {
         e.printStackTrace();
      }

      // Step 2: Read with skipping bytes
      try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
         System.out.println("First int: " + ois.readInt()); // Reads 10

         int skipped = ois.skipBytes(4); // Attempt to skip next 4 bytes (should skip 20)
         System.out.println("Bytes actually skipped: " + skipped);

         if (skipped == 4) { // Ensure skipping was successful
            System.out.println("Second int (after skipping): " + ois.readInt()); // Reads 30
         } else {
            System.out.println("Not enough bytes left to skip.");
         }
      } catch (EOFException e) {
         System.out.println("Reached the end of the file unexpectedly.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Objects written to file.
First int: 10
Bytes actually skipped: 4
Second int (after skipping): 30

Explanation

  • Check how many bytes were actually skipped by capturing the return value of skipBytes(int len).

  • Ensure there are enough bytes left before reading.

java_io_objectinputstream.htm
Advertisements