日期:2014-05-20  浏览次数:20753 次

IO流问题
各位帮小弟看看我这小测试程序的错误之处!
import java.io.*;

public class TestDateStream {
public static void main(String[] args) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DateOutputStream dos = new DateOutputStream(baos);
DataOutputStream dos = new DataOutputStream(baos);
try {
baos.writeDouble(Math.random());
baos.writeBoolean(true);

ByteArrayInputStream bais = new ByteArrayInoutStream(baos.toByteArray());
System.out.println(bais.available());

DateInputStream dis = new DateInputStream(bais);

System.out.println(dis.readDouble());
System.out.println(dis.readBoolean());

dos.close();
dis.close();


} catch(IOException e) {
e.printStackTrace();
}
}
}

------解决方案--------------------
DateOutputStream dos = new DateOutputStream(baos);
DataOutputStream dos = new DataOutputStream(baos);

为啥弄两遍?着肯定错啊。
------解决方案--------------------
LZ全是拼写错误啊,注释部分是你的。
Java code


import java.io.*;

public class TestDateStream {
    public static void main(String[] args) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        //DateOutputStream dos = new DateOutputStream(baos);
        DataOutputStream dos = new DataOutputStream(baos);
        try {
        //    baos.writeDouble(Math.random());
        //    baos.writeBoolean(true);
            dos.writeDouble(Math.random());
            dos.writeBoolean(true);

            //ByteArrayInputStream bais = new ByteArrayInoutStream(
            ByteArrayInputStream bais = new ByteArrayInputStream(
                    baos.toByteArray());
            System.out.println(bais.available());

            //DateInputStream dis = new DateInputStream(bais);
            DataInputStream dis = new DataInputStream(bais);

            System.out.println(dis.readDouble());
            System.out.println(dis.readBoolean());

            dos.close();
            dis.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}