Can we use readUTF() to read a string from a .txt file in Java?


The readUTF() method of the java.io.DataOutputStream reads data that is in modified UTF-8 encoding, into a String and returns it.

Example

The following Java program reads a UTF-8 text from a .txt file using the readUTF() method.

import java.io.DataInputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.IOException;
public class UTF8Example {
   public static void main(String args[]) {
      StringBuffer buffer = new StringBuffer();
      try {
         //Instantiating the FileInputStream class
         FileInputStream fileIn = new FileInputStream("D:\test.txt");
         //Instantiating the DataInputStream class
         DataInputStream inputStream = new DataInputStream(fileIn);
         //Reading UTF data from the DataInputStream
         while(inputStream.available()>0) {
            buffer.append(inputStream.readUTF());
         }
      }
      catch(EOFException ex) {
         System.out.println(ex.toString());
      }
      catch(IOException ex) {
         System.out.println(ex.toString());
      }
      System.out.println("Contents of the file: "+buffer.toString());
   }
}

Output

Contents of the file: టుటోరియల్స్ పాయింట్ కి స్వాగతిం

Using readUTF() method to read normal text

While reading text from a file using the readUTF() method, if the contents of the file are not invalid UTF format then this method generates an EOFException.

Example

In the following Java program, we are writing normal text into a file using the BufferedWriter and trying to read it using the readUTF() method. This generates an EOFException.

import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
import java.io.FileWriter;
public class ReadingTextUsingUTF {
   public static void main(String args[]) {
      FileWriter fileOut = null;
      BufferedWriter bufferedWriter = null;
      FileInputStream fileIn = null;
      DataInputStream inputStream = null;
      Scanner sc = new Scanner(System.in);
      try {
         //Instantiating the FileOutputStream class
         fileOut = new FileWriter("D:\utfText.txt");
         //Instantiating the DataOutputStream class
         bufferedWriter = new BufferedWriter(fileOut);
         //Writing UTF data to the output stream
         System.out.println("Enter sample text (single line)");
         bufferedWriter.write(sc.nextLine());
         bufferedWriter.flush();
         System.out.println("Data inserted into the file");
         bufferedWriter.close();
         fileOut.close();
         //Instantiating the FileInputStream class
         fileIn = new FileInputStream("D:\utfText.txt");
         //Instantiating the DataInputStream class
         inputStream = new DataInputStream(fileIn);
         //Reading UTF data from the DataInputStream
         while(inputStream.available()>0) {
            System.out.println(inputStream.readUTF());
         }
         inputStream.close();
         fileIn.close();
      }
      catch(EOFException ex) {
         System.out.println("Contents are not in valid UTF-8 format");
      }
      catch(IOException ex) {
         System.out.println(ex.toString());
      }
   }
}

Output

Enter sample text (single line)
Hello how are you]
Data inserted into the file
Contents are not in valid UTF-8 format

Updated on: 10-Sep-2019

254 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements