- 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 - ObjectInputStream readUTF() method
Description
The Java ObjectInputStream readUTF() method reads a UTF-8 encoded string from the input stream. It is useful when dealing with serialized text data in a binary format.
Declaration
Following is the declaration for java.io.ObjectInputStream.readUTF() method.
public String readUTF()
Parameters
NA
Return Value
This method returns the String.
Exception
UTFDataFormatException − If read bytes do not represent a valid modified UTF-8 encoding of a string.
IOException − If there are I/O errors while reading from the underlying InputStream.
Example - Usage of ObjectInputStream readUTF() method
The following example shows the usage of Java ObjectInputStream readUTF() method.
ObjectInputStreamDemo.java
package com.tutorialspoint;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class ObjectInputStreamDemo {
public static void main(String[] args) {
String s = "Hello World!";
try {
// create a new file with an ObjectOutputStream
FileOutputStream out = new FileOutputStream("test.txt");
ObjectOutputStream oout = new ObjectOutputStream(out);
// write something in the file
oout.writeUTF(s);
oout.writeUTF("This is an example");
oout.flush();
// create an ObjectInputStream for the file we created before
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));
// read and print the string
System.out.println("" + ois.readUTF());
// read and print the string
System.out.println("" + ois.readUTF());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Output
Let us compile and run the above program, this will produce the following result−
Hello World! This is an example
Example - Reading a Single UTF String
The following example shows the usage of Java ObjectInputStream readUTF() method. This example writes a single UTF-8 string to a file and reads it back using readUTF().
ObjectInputStreamDemo.java
package com.tutorialspoint;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class ObjectInputStreamDemo {
public static void main(String[] args) {
String filename = "utfData.bin";
// Writing a UTF string to a file
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
oos.writeUTF("Hello, World!"); // Writing a UTF-8 encoded string
System.out.println("UTF string written to file.");
} catch (IOException e) {
e.printStackTrace();
}
// Reading the UTF string using ObjectInputStream
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
String message = ois.readUTF(); // Reads the UTF-8 string
System.out.println("Read UTF string: " + message);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output
Let us compile and run the above program, this will produce the following result−
UTF string written to file. Read UTF string: Hello, World!
Explanation
Writes a UTF-8 encoded string ("Hello, World!") to a binary file using writeUTF().
readUTF() reads the string correctly without character corruption.
This is useful for cross-platform compatibility since UTF-8 supports international characters.
Example - Reading Multiple UTF Strings from a File
The following example shows the usage of Java ObjectInputStream readUTF() method. This example writes multiple UTF-8 strings to a file and reads them back.
ObjectInputStreamDemo.java
package com.tutorialspoint;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class ObjectInputStreamDemo {
public static void main(String[] args) {
String filename = "multiUtfData.bin";
// Writing multiple UTF strings to a file
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
oos.writeUTF("Java Programming");
oos.writeUTF("ããã«ã¡ã¯ (Hello in Japanese)");
oos.writeUTF("Hola, ¿cómo estás? (Spanish)");
System.out.println("Multiple UTF strings written to file.");
} catch (IOException e) {
e.printStackTrace();
}
// Reading the UTF strings using ObjectInputStream
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
System.out.println("Reading UTF strings:");
for (int i = 0; i < 3; i++) {
String message = ois.readUTF(); // Reads each UTF-8 string
System.out.println("String " + (i + 1) + ": " + message);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output
Let us compile and run the above program, this will produce the following result−
Multiple UTF strings written to file. Reading UTF strings: String 1: Java Programming String 2: ããã«ã¡ã¯ (Hello in Japanese) String 3: Hola, ¿cómo estás? (Spanish)
Explanation
Writes three UTF-8 encoded strings: "Java Programming" , "ããã«ã¡ã¯ (Hello in Japanese)" , "Hola, ¿cómo estás? (Spanish)".
readUTF() reads each string in the correct order.
This ensures correct character encoding for international text.