DataInputStream readUTF(DataInput) in Java

readUTF(DataInput): This method is available in the java.io.DataInputStream class of Java.

Syntax:

String java.io.DataInputStream.readUTF(DataInput in) throws IOException

This method takes one argument. This method reads from the stream in a representation of a Unicode character string encoded in modified UTF-8 format; this string of characters is then returned as a String.

The details of the modified UTF-8 representation are exactly the same as for the readUTF method of DataInput.

Parameters: One parameter is required for this method.

in: a data input stream.

Returns: a Unicode string.

Throws:

1. EOFException - if the input stream reaches the end before all the bytes.

2. IOException - the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs.

3. UTFDataFormatException - if the bytes do not represent a valid modified UTF-8 encoding of a Unicode string.

Approach

Java

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class DataInputStreamreadUTF2 {
    public static void main(String[] args) throws IOException {

        InputStream in = new FileInputStream("D:\\hello.txt");
        DataInputStream dataInputStream =
new DataInputStream(in);

        System.out.println(DataInputStream.readUTF(
dataInputStream));
        dataInputStream.close();
    }
}

Output:

Exception in thread "main" java.io.EOFException at java.base/java.io.DataInputStream.readFully(DataInputStream.java:201) at java.base/java.io.DataInputStream.readUTF(DataInputStream.java:613)


No comments:

Post a Comment