为什么下面这段程序
- import java.io.*;
- class DataStreamDemo
- {
- public static void main(String[] args)throws IOException
- {
- DataOutputStream dos=new DataOutputStream(new FileOutputStream("111.txt"));
- DataInputStream dis=new DataInputStream(new FileInputStream("111.txt"));
- dos.writeInt(123);
- dos.writeDouble(77.654);
- int i=dis.readInt();
- double d=dis.readDouble();
- System.out.println(i);
- System.out.println(d);
- dos.close();
- dis.close();
- }
- }
复制代码 先成功运行,可以打印出i 和d的值。第二次的时候我把两个write语句注释掉。再运行就会出现异常呢?
- import java.io.*;
- class DataStreamDemo
- {
- public static void main(String[] args)throws IOException
- {
- DataOutputStream dos=new DataOutputStream(new FileOutputStream("111.txt"));
- DataInputStream dis=new DataInputStream(new FileInputStream("111.txt"));
- //dos.writeInt(123);
- //dos.writeDouble(77.654);
- int i=dis.readInt();
- double d=dis.readDouble();
- System.out.println(i);
- System.out.println(d);
- dos.close();
- dis.close();
- }
- }
复制代码 第二次运行的时候111.txt里面不是已经被写入数据了吗?,为什么读取还会出异常呢?,异常如下
Exception in thread "main" java.io.EOFException
at java.io.DataInputStream.readInt(DataInputStream.java:392)
at DataStreamDemo.main(DataStreamDemo.java:12)
|
|