Java - RandomAccessFile skipBytes(int n) method



Description

The Java RandomAccessFile skipBytes(int n) method attempts to skip over n bytes of input discarding the skipped bytes. This method may skip over some smaller number of bytes, possibly zero. This may result from any of a number of conditions; reaching end of file before n bytes have been skipped is only one possibility. This method never throws an EOFException. The actual number of bytes skipped is returned. If n is negative, no bytes are skipped.

Declaration

Following is the declaration for java.io.RandomAccessFile.skipBytes(int n) method.

public int skipBytes(int n)

Parameters

n − The number of bytes to be skipped.

Return Value

This method returns the actual number of bytes skipped.

Exception

  • IOException − If an I/O error occurs.

Example - Usage of RandomAccessFile skipBytes(int n) method

The following example shows the usage of RandomAccessFile skipBytes(int n) method.

RandomAccessFileDemo.java

package com.tutorialspoint;

import java.io.RandomAccessFile;
import java.io.IOException;

public class RandomAccessFileDemo {
   public static void main(String[] args) {
   
      try {
         // create a new RandomAccessFile with filename test
         RandomAccessFile raf = new RandomAccessFile("test.txt", "rw");

         // write something in the file
         raf.writeUTF("Hello World");

         // set the file pointer at 0 position
         raf.seek(0);

         // print the string
         System.out.println(raf.readUTF());

         // set the file pointer at 0 position
         raf.seek(0);

         // attempt to skip 10 bytes and print the number of bytes skipped
         System.out.println(raf.skipBytes(10));

         // print what is left after skipping
         System.out.println(raf.readLine());

         // set the file pointer to position 8
         raf.seek(8);

         // attempt to skip 10 more bytes and print the number of bytes skipped
         System.out.println(raf.skipBytes(10));
         
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

Output

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 −

ABCDE

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

Hello World
10
rld
5

Example - Skipping Bytes Before Reading

The following example shows the usage of RandomAccessFile skipBytes(int n) method.

RandomAccessFileDemo.java

package com.tutorialspoint;

import java.io.RandomAccessFile;
import java.io.IOException;

public class RandomAccessFileDemo {
   public static void main(String[] args) {
      try {
         RandomAccessFile raf = new RandomAccessFile("skip1.dat", "rw");

         // Write 10 characters: A to J
         raf.writeBytes("ABCDEFGHIJ");

         // Move to the beginning
         raf.seek(0);

         // Skip the first 5 bytes (A to E)
         raf.skipBytes(5);

         // Read the next byte (should be 'F')
         int b = raf.read();
         System.out.println("Byte after skipping 5: " + (char) b); // Output: F

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

Output

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

Byte after skipping 5: F

Explanation

  • skipBytes(5) skips the first 5 bytes in the file.

  • The next read() picks up from byte 6 ('F').

  • Useful for skipping headers or metadata in binary/text files.

Example - Skipping Between Multiple Reads

The following example shows the usage of RandomAccessFile skipBytes(int n) method.

RandomAccessFileDemo.java

package com.tutorialspoint;

import java.io.RandomAccessFile;
import java.io.IOException;

public class RandomAccessFileDemo {
   public static void main(String[] args) {
      try {
         RandomAccessFile raf = new RandomAccessFile("skip2.dat", "rw");

         // Write 3 groups of data
         raf.writeBytes("AAA"); // Group 1
         raf.writeBytes("BBB"); // Group 2
         raf.writeBytes("CCC"); // Group 3

         // Reset pointer
         raf.seek(0);

         // Read first 3 bytes (AAA)
         byte[] first = new byte[3];
         raf.readFully(first);
         System.out.println("First group: " + new String(first)); // AAA

         // Skip next 3 bytes (BBB)
         raf.skipBytes(3);

         // Read last group (CCC)
         byte[] last = new byte[3];
         raf.readFully(last);
         System.out.println("Last group: " + new String(last)); // CCC

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

Output

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

First group: AAA
Last group: CCC

Explanation

  • After reading the first group (AAA), we skip over the second group (BBB) using skipBytes(3).

  • Then we read the third group (CCC).

  • Perfect for selective reading in structured or chunked binary files.

java_io_randomaccessfile.htm