Java - RandomAccessFile readShort() method



Description

The Java RandomAccessFile readShort() method reads a signed 16-bit number from this file. The method reads two bytes from this file, starting at the current file pointer.

Declaration

Following is the declaration for java.io.RandomAccessFile.readShort() method.

public final short readShort()

Parameters

NA

Return Value

This method returns the next two bytes of this file, interpreted as a signed 16-bit number.

Exception

  • IOException − If an I/O error occurs.

  • EOFException − If this file reaches the end before reading two bytes.

Example - Usage of RandomAccessFile readShort() method

The following example shows the usage of RandomAccessFile readShort() method.

RandomAccessFileDemo.java

package com.tutorialspoint;

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

public class RandomAccessFileDemo {
   public static void main(String[] args) {

      try {
         short s = 15000;
         
         // create a new RandomAccessFile with filename test
         RandomAccessFile raf = new RandomAccessFile("test.txt", "rw");

         // write something in the file
         raf.writeShort(s);

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

         // print the short
         System.out.println(raf.readShort());

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

         // write something in the file
         raf.writeShort(134);

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

         // print the short
         System.out.println(raf.readShort());
         
      } 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 −

15000
134

Example - Writing and Reading short Values

The following example shows the usage of RandomAccessFile readShort() 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("shorts1.dat", "rw");

         // Write two short values (2 bytes each)
         raf.writeShort(12345);
         raf.writeShort(32000);

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

         // Read the short values
         short s1 = raf.readShort();
         short s2 = raf.readShort();

         System.out.println("First short: " + s1);  // 12345
         System.out.println("Second short: " + s2); // 32000

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

Output

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

First short: 12345
Second short: 32000

Explanation

  • writeShort(short s) writes 2 bytes to the file.

  • readShort() reads 2 bytes and returns a short value.

  • Useful for reading compact numeric data like flags, codes, or small integers.

Example - Random Access to a Specific short

The following example shows the usage of RandomAccessFile readShort() 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("shorts2.dat", "rw");

         // Write 3 short values (each 2 bytes)
         raf.writeShort(100);  // Position 0
         raf.writeShort(200);  // Position 2
         raf.writeShort(300);  // Position 4

         // Jump to the second short (offset = 2 bytes)
         raf.seek(2);
         short second = raf.readShort();

         System.out.println("Second short: " + second); // 200
      
         raf.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Second short: 200

Explanation

  • Each short is 2 bytes.

  • seek(2) moves the file pointer to the second short.

  • readShort() reads the value at that specific position.

  • Shows how you can randomly access small fixed-size values in a file.

java_io_randomaccessfile.htm
Advertisements