-1

I use the datainputstream.readUTF() method to read a text file with data, always EOF Exception, I do not understand why this is?

public void readDataDemo() throws IOException {
        RandomAccessFile randomAccessFile = new RandomAccessFile("temp/buf.txt", "rw");
        String str = randomAccessFile.readUTF();
        randomAccessFile.close();
    }
yang
  • 19
  • 2
  • 1
    That method is not for reading text files. [Read the DataInput documentation](https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/io/DataInput.html#readUTF%28%29). (You probably want [Files.readString](https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/nio/file/Files.html#readString%28java.nio.file.Path%29).) – VGR Jan 11 '20 at 03:24
  • There are many related questions on SO for what you're looking for. [This is one](https://stackoverflow.com/questions/4716503/reading-a-plain-text-file-in-java), [this is another one](https://stackoverflow.com/questions/41166223/java-read-file-using-scanner), and [here's one more](https://stackoverflow.com/questions/5868369/how-to-read-a-large-text-file-line-by-line-using-java). – Kaan Jan 11 '20 at 03:38

1 Answers1

4

DataInputStream.readUTF is not suitable for reading text files.

It is actually designed for reading character strings that have been embedded in (binary) data files. What the method expects is a 2 byte binary unsigned integer which gives the length (in bytes) of the UTF encoded string. This is followed by the bytes of "modified UTF-8" data. See the following links for more details:

If you try to use DataInputStream.readUTF() to read a regular text file, it will misinterpret the first 2 characters in the file as a byte count and then attempt to read that number of bytes. Depending on the (supposed) count and the file size, that is liable to attempt to read past the end of the file ... resulting in an EOF exception.

Here are some Q&A's that explain some of the correct ways to read text from a text file:

But note that none of them involve DataInputStream. That class is for reading binary data files, not text files.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216