本帖最后由 viliv715 于 2014-9-10 14:51 编辑
毕老师讲解的DataStream中,按照代码写下来有两个问题。我安装的jdk是1.7的。1、writeData方法写出来的不是13个字节,是十个字节。
2、readData方法报异常
Exception in thread "main" java.io.EOFException
at java.io.DataInputStream.readFully(DataInputStream.java:197)
at java.io.DataInputStream.readLong(DataInputStream.java:416)
at java.io.DataInputStream.readDouble(DataInputStream.java:468)
at day21.DataStreamDemo.readData(DataStreamDemo.java:22)
at day21.DataStreamDemo.main(DataStreamDemo.java:13)
代码如下:
- import java.io.*;
- /*
- DataInputStream和DataOutputStream:
- 可以用作操作基本数据类型数据的流对象。
- */
- public class DataStreamDemo {
- public static void main(String[] args) throws IOException{
- // writeData();
- readData();
- }
- public static void readData() throws IOException
- {
- DataInputStream dis = new DataInputStream(new FileInputStream("data.txt"));
-
- int num = dis.readInt();
- boolean b = dis.readBoolean();
- double d = dis.readDouble();
-
- System.out.println(num);
- System.out.println(b);
- System.out.println(d);
- dis.close();
- }
- public static void writeData() throws IOException
- {
- DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));
-
- //dos.write(234); 这里出错了,大意了。
复制代码
|
|