Java - RandomAccessFile readUTF() method



Description

The Java RandomAccessFile readUTF() method reads in a string from this file.

Declaration

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

public final String readUTF()

Parameters

NA

Return Value

This method returns a Unicode string.

Exception

  • IOException − If an I/O error occurs.

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

  • UTFDataFormatException − If the bytes do not represent valid modified UTF-8 encoding of a Unicode string.

Example - Usage of RandomAccessFile readUTF() method

The following example shows the usage of RandomAccessFile readUTF() 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);

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

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

         // print the string
         System.out.println(raf.readUTF());
         
      } 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
This is an example

Example - Writing and Reading a Single UTF String

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

         // Write a UTF string
         raf.writeUTF("Hello, Java!");

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

         // Read the UTF string
         String text = raf.readUTF();

         System.out.println("Read string: " + text); // Output: Hello, Java!

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

Output

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

Read string: Hello, Java!

Explanation

  • writeUTF(String s) writes the string in modified UTF-8 format, which includes a 2-byte length prefix.

  • readUTF() reads the 2-byte length and then decodes the string.

  • This method ensures proper reading of special or non-ASCII characters.

Example - Reading Multiple UTF Strings from File

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

         // Write multiple UTF strings      
         raf.writeUTF("Apple");
         raf.writeUTF("Banana");
         raf.writeUTF("Cherry");

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

         // Read the UTF strings
         String s1 = raf.readUTF();
         String s2 = raf.readUTF();
         String s3 = raf.readUTF();

         System.out.println("First: " + s1);  // Apple
         System.out.println("Second: " + s2); // Banana
         System.out.println("Third: " + s3);  // Cherry

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

Output

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

First: Apple
Second: Banana
Third: Cherry

Explanation

  • Each call to writeUTF() stores a UTF-encoded string with a length prefix.

  • readUTF() reads each string one by one, matching the write order.

  • Perfect for reading structured text data like names, messages, or keys.

java_io_randomaccessfile.htm