Java - RandomAccessFile readUnsignedByte() method



Description

The Java RandomAccessFile readUnsignedByte() method reads an unsigned eight-bit number from this file. This method reads a byte from this file, starting at the current file pointer, and returns that byte.

Declaration

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

public final int readUnsignedByte()

Parameters

NA

Return Value

This method returns the next byte of this file, interpreted as an unsigned eight-bit number.

Exception

  • IOException − If an I/O error occurs.

  • EOFException − If this file has reached the end.

Example - Usage of RandomAccessFile readUnsignedByte() method

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

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

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

0
32

Example - Reading Unsigned Byte as an Int (0–255)

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

         // Write bytes (values beyond 127 will be negative if read with readByte())
         raf.writeByte(130); // signed byte would show -126

         // Reset pointer
         raf.seek(0);

         // Read as unsigned byte
         int value = raf.readUnsignedByte();

         System.out.println("Unsigned byte value: " + value); // 130

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

Output

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

Unsigned byte value: 130

Explanation

  • writeByte(130) stores the byte 0x82.

  • If we used readByte(), we'd get -126 due to Java’s signed byte system.

  • readUnsignedByte() correctly reads it as an int in the range 0–255 (130 in this case).

  • This is crucial when working with raw binary data or protocols that treat bytes as unsigned.

Example - Reading a Sequence of Unsigned Bytes

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

            // Write bytes: 0, 127, 255 (use 255 via write method because writeByte(-1) = 255)
            raf.write(new byte[]{0, 127, (byte) 255});

            // Reset pointer
            raf.seek(0);

            // Read and print all three as unsigned
            for (int i = 0; i < 3; i++) {
                int value = raf.readUnsignedByte();
                System.out.println("Byte " + i + ": " + value);
            }

            // Output:
            // Byte 0: 0
            // Byte 1: 127
            // Byte 2: 255

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

Output

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

Byte 0: 0
Byte 1: 127
Byte 2: 255

Explanation

  • The byte 255 (0xFF) would appear as -1 with readByte().

  • readUnsignedByte() reads it as 255, which is correct for unsigned byte logic.

  • This is useful when handling image data, encryption keys, or file formats like PNG, where byte values can go up to 255.

java_io_randomaccessfile.htm
Advertisements