Java - RandomAccessFile readUnsignedShort() method



Description

The Java RandomAccessFile readUnsignedShort() method reads an unsigned 16-bit number from this file. This method reads two bytes from the file, starting at the current file pointer.

Declaration

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

public final int readUnsignedShort()

Parameters

NA

Return Value

This method returns the next byte of this file, interpreted as an unsigned 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 readUnsignedShort() method

The following example shows the usage of RandomAccessFile readUnsignedShort() 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 short
         System.out.println(raf.readUnsignedShort());

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

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

11 
8279

Example - Reading an Unsigned Short (0 to 65535)

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

         // Write a short value above 32767 (Java's max for signed short)
         raf.writeShort(60000); // This actually writes a signed short with overflow

         // Reset pointer to beginning
         raf.seek(0);

         // Read as unsigned short
         int value = raf.readUnsignedShort();

         System.out.println("Unsigned short value: " + value); // Output: 60000

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

Output

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

Unsigned short value: 60000

Explanation

  • writeShort(60000) writes two bytes representing the short, though 60000 overflows short (max 32767).

  • readUnsignedShort() interprets those bytes as an unsigned 16-bit value and correctly returns 60000 as an int.

  • Useful when working with protocols or formats that treat 2-byte numbers as unsigned (e.g., network headers, binary formats).

Example - Reading a Sequence of Unsigned Shorts

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

         // Write a sequence of short values
         raf.writeShort(1000);
         raf.writeShort(50000);
         raf.writeShort(65535); // maximum 2-byte unsigned value

         // Reset file pointer
         raf.seek(0);

         // Read and print each as unsigned short
         for (int i = 0; i < 3; i++) {
            int val = raf.readUnsignedShort();
            System.out.println("Unsigned short " + i + ": " + val);
         }

         // Output:
         // Unsigned short 0: 1000
         // Unsigned short 1: 50000
         // Unsigned short 2: 65535

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

Output

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

Unsigned short 0: 1000
Unsigned short 1: 50000
Unsigned short 2: 65535

Explanation

  • Each short is 2 bytes. writeShort() stores the value in binary.

  • readUnsignedShort() returns those 2 bytes as an int in the range 0−65535, no matter the sign bit.

  • This method is ideal for reading unsigned 16-bit values from structured binary data (like image headers, file IDs, etc.).

java_io_randomaccessfile.htm
Advertisements