DataInputStream readUTF() in Java

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

Syntax:

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

This method sees the general contract of the readUTF method of DataInput.

Bytes for this operation are read from the contained input stream.

Parameters: NA

Returns: a Unicode string.

Throws:

1. EOFException - if this input stream reaches the end before reading 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 string.

Approach

Java

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

public class DataInputStreamreadUTF {
    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.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) at java.base/java.io.DataInputStream.readUTF(DataInputStream.java:568)


No comments:

Post a Comment