无法在java IO中读取数据输入流

时间:2014-10-18 05:09:42

标签: java io file-handling

大家好我正在练习 java数据输入输出流。但我不知道如何解决这个问题我可以将数据输入流写入我的文件,但无法读取它

这是我的代码:

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();
                }
            }
        }

,这是错误:

 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

请帮助我为什么我无法读取数据输入流

1 个答案:

答案 0 :(得分:1)

dataOut.write(i)只写一个字节。但是你正在读inti = dataIn.readInt(),这是4个字节。

因此,当尝试读取最后一个double元素时,在尝试读取8个字节(double的长度)时,流中只有5个字节,因此异常。

由于您要撰写/阅读int,因此需要致电dataOut.writeInt(i)而不是dataOut.write(i)