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.

java_io_objectinputstream.htm
Advertisements
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy