
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
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.