Java.io.DataInputStream.readUTF() Method

Advertisements


Description

The java.io.DataInputStream.readUTF(DataInput in) method reads in a string that has been encoded using a modified UTF-8 format.

Declaration

Following is the declaration for java.io.DataInputStream.readUTF(DataInput in) method:

public static final String readUTF(DataInput in)

Parameters

  • NA

Return Value

This method returns an= Unicode string

Exception

  • EOFException

  • UTFDataFormatException

  • IOException -- if an I/O error occurs..

Example

The following example shows the usage of java.io.DataInputStream.readUTF(DataInput in) method.

package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.IOException;

public class FileOutputStreamAvailable extends FileOutputStream {
   public FileOutputStreamAvailable() throws Exception {
      super("C://test.txt");
   }

   public static void main(String[] args) throws IOException {
      
      FileOutputStream fos = null;
      FileOutputStreamAvailable fosa = null;

      try{
         // create new File input stream
         fosa = new FileOutputStreamAvailable();
         
         // read byte from file input stream
         fosa.finalize();
         
         // converts int to char
         System.out.println("Stream is closed successfully.");
         
      }catch(Exception ex){
         // if any error occurs
         ex.printStackTrace();
      }finally{
         
         // releases all system resources from the streams
         if(fos!=null)
            fos.close();
      }
   }
}

Assuming we have a text file c:/test.txt, 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:

Stream is closed successfully.



Advertisements
Advertisements