2

Hi everyone i am practicing with java data input output stream. But i don't know how to fix this problem i can write data input stream to my file but can't read it

here is my code:

public static void readDataIOStream(){
            DataInputStream dataIn = null;

            int i = 10;
            double d = 1023.56;
            boolean b = true;

            try {
                dataIn = new DataInputStream(
                        new FileInputStream("test.txt"));

                i = dataIn.readInt();
                System.out.println("Reading " + i);
                d = dataIn.readDouble();
                System.out.println("Reading " + d);
                b = dataIn.readBoolean();
                System.out.println("Reading " + b);
                d = dataIn.readDouble();
                System.out.println("Reading " + d);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }try {
                dataIn.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        public static void writeDataIOStream(){
            DataOutputStream dataOut = null;

            int i = 10;
            double d = 1023.56;
            boolean b = true;

            try {
                dataOut = new DataOutputStream(new FileOutputStream("test.txt"));
                System.out.println("Writing " + i);
                dataOut.write(i);
                System.out.println("Writing " + d);
                dataOut.writeDouble(d);
                System.out.println("Writing " + b);
                dataOut.writeBoolean(b);
                System.out.println("Writing " + 12.2 * 7.4);
                dataOut.writeDouble(12.2 * 7.4);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                try {
                    dataOut.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

and here is error :

 java.io.EOFException
    at java.io.DataInputStream.readFully(Unknown Source)
    at java.io.DataInputStream.readLong(Unknown Source)
    at java.io.DataInputStream.readDouble(Unknown Source)
    at bytestream.DataIOStream.readDataIOStream(DataIOStream.java:108)
    at bytestream.DataIOStream.main(DataIOStream.java:16)
Writing 10
Writing 1023.56
Writing true
Writing 90.28
Reading 172003324
Reading 8.029891292620447E283
Reading true

Please help me why i can't read Data Input stream

1 Answer 1

2

dataOut.write(i) only writes one byte. Yet you are reading an int doing i = dataIn.readInt() which is 4 bytes.

Therefore when trying to read the last element which is a double, there is only 5 bytes in the stream while it is trying to read 8 bytes (the length of a double), hence the exception.

Since you want to write/read an int, you need to call dataOut.writeInt(i) instead of dataOut.write(i).

Sign up to request clarification or add additional context in comments.

1 Comment

you are missing instead of dataOut.write(i);

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.