1

I'm trying to familiarize myself with Java IO classes, so I wrote the code below:

public static void main(String[] args)throws IOException {
    FileOutputStream fos = new FileOutputStream("fileIO.txt");
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    DataOutputStream dos = new DataOutputStream(bos);

    //fos.write(9999);
    //bos.write(9999);
    dos.writeInt(9999);
    dos.writeBytes("中文字(Chinese)\n");
    dos.writeChars("中文字(Chinese)\n");
    dos.flush();

    FileInputStream fis = new FileInputStream("fileIO.txt");
    BufferedInputStream bis = new BufferedInputStream(fis);
    DataInputStream dis = new DataInputStream(bis);

    System.out.println(dis.readInt());
    System.out.println(dis.readUTF());
}

Unfortunately, I get this:

9999
Exception in thread "main" java.io.EOFException
at java.io.DataInputStream.readFully(DataInputStream.java:180)
at java.io.DataInputStream.readUTF(DataInputStream.java:592)
at java.io.DataInputStream.readUTF(DataInputStream.java:547)
at IO.main(IO.java:34)

Could anyone point out why? Thanks.

5 Answers 5

4

Instead of

dos.writeBytes("中文字(Chinese)\n");
dos.writeChars("中文字(Chinese)\n");

you need

dos.writeUTF("中文字(Chinese)\n");
Sign up to request clarification or add additional context in comments.

1 Comment

I see, so you can throw in a lot of bytes and character with writeBytes & writeChars but only read one at a time with their repective read operations?
3

When you perform a readUTF, the first two byte are used for the length. This means if you have random bytes there (not from writeUTF) you will attempt to read very long string instead and as there is not enough data, you will get EOFException.

1 Comment

I see! I suppose this also explains the suspicious blank space ' ' in between every English letter when using writeChar. ex: ( C h i n e s e )
1

You can only use readUTF() to read items that were written with writeUTF().

This is true in general for readXXX() and writeXXX() for any XXX (unless you want to read the bytes of an int or some such and you know what you're doing).

Comments

1

I think this link will be helpful. The exception is thrown (from oracle docs) -

if this input stream reaches the end before reading all the bytes.

2 Comments

+1 for the diagram at the top. I find it to be of much greater importance. I wonder why they didn't copy & paste it to DataOutputStream, which I read, and missed out writeUTF.
@SomeNoobStudent Because DataOutputStream() doesn't throw EOFExceptions?
0

You haven't UTF char after Integer number in your file. When you're trying read UTF there is End of File so you have the exception.

Try in debug mode stop before you read and check manually to your file, what do you have then?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.