Java之I/O(2-OutputStream类及其子类)

来源:互联网 发布:淘宝上好评后怎么截图 编辑:程序博客网 时间:2024/05/18 18:44

OutputStream与InputStream一样,都是抽象类,是输出字节流的所有类的超类。它的作用是接收输出字节并将这些字节输送到某个接收的地方。根据输出字节的目的地的不同可以将OutputStream的子类分为以下几种:

OutputStream子类           解释ByteArrayOutputStream     该类实现了一个输出流,其数据被写入由byte数组充当的缓冲区,缓冲区会随着数据的不断写入而自动增长。 FileOutputStream          该类实现了一个输出流,其数据写入文件。ObjectOutputStream        该类将实现了序列化的对象序列化后写入指定地方。  PipedOutputStream         管道的输出流,是管道的发送端。

抽象类OutputStream的主要方法有:

方法                       解释close()                   关闭此输出流并释放相应资源flush()                   刷新此输出流并强制写出所有缓冲的输出字节。write(byte[])             将字节写入此输出流。write(byte[],int,int)    将指定的若干字节写入此输出流。write(int)                将指定若干字节写入此输出流。

下面举几个例子(例子中主要考虑各输出类的用法,异常与释放资源暂不考虑):
1 ByteArrayOutputStream

    public void testByteArray(){    /* 创建一个新的byte数组输出流,参数指定缓冲区大小(以字节为单位),若使用无餐构造器,缓冲区默认大小是32字节。*/        ByteArrayOutputStream out=new ByteArrayOutputStream(100);    /* size()方法返回此输出流当前有效字节的个数。*/        System.out.println("Now the byte size of buffer:"+out.size());    /* toString()方法将缓冲区内容转换为字符串。*/        System.out.println("Now the content of the out: "+out.toString());        byte b=97;    /* 将指定字节写入此输出流。*/        out.write(b);        System.out.println("Now the byte size of buffer:"+out.size());        System.out.println("Now the content of the out: "+out.toString());    /* reset()方法将输出流的有效字节个数重新置为0,意味着丢弃输出流目前积累的所有输出。重新使用已分配的缓冲区空间。*/        out.reset();        System.out.println("Now the byte size of buffer:"+out.size());        System.out.println("Now the content of the out: "+out.toString());    }/*Output:Now the byte size of buffer:0Now the content of the out: Now the byte size of buffer:1Now the content of the out: aNow the byte size of buffer:0Now the content of the out: /*

2 FileOutputStream

    public void testFile() throws IOException{        File f=new File("testFileOut.txt");        FileOutputStream out=new FileOutputStream(f);        out.write("hello ,test the string".getBytes());     /*    Content of testFileOut.txt:    hello ,test the string    /*    }

3 ObjectOutputStream
ObjectOutputStream、ObjectInputStream经常会和FileInputStream、FileOutputStream一起用,ObjectOutputStream用于将对象序列化写入文件,ObjectInputStream将对象反序列化读出(当然对象必须是实现了序列化的)。

    public void testObject() {        Student s1=new Student("zdd1", 1);        Student s2=new Student("zdd2", 2);        FileOutputStream fout=new FileOutputStream("student.txt");        /* ObjectOutputStream的参数可以是任何继承于OutputStream的子类,即写入任何指定的OutputStream都可以,ObjectInputStream也是如此。*/        ObjectOutputStream oout=new ObjectOutputStream(fout);        oout.writeObject(s1);        oout.writeObject(s2);        FileInputStream fin=new FileInputStream("student.txt");        ObjectInputStream oin=new ObjectInputStream(fin);        Student s3,s4;        s3=(Student) oin.readObject();        s4=(Student) oin.readObject();        System.out.println("s3's info: "+s3.name+" "+s3.id+"\n"+"s4's info: "+s4.name+" "+s4.id);    }/*Output:s3's info: zdd1 1s4's info: zdd2 2Note:Student类已经实现Serializable*/

4 PipedOutputStream
PipedOutputStream多与PipedInputStream连用,可认为连接成一个管道,前者用于向管道中写入内容,后者用于从管道中读取内容。

    public void testPipe() throws IOException{        PipedInputStream pipeSnk=new PipedInputStream();        PipedOutputStream pipeSrc=new PipedOutputStream();        /* 也可以使用构造函数来绑定管道*/        pipeSrc.connect(pipeSnk);        pipeSrc.write("Test pipe".getBytes());        int flag=0;        while(flag!=(-1)){            flag=pipeSnk.read();            System.out.println(flag);        }    }    /*    Output:    84 101 115 116 32 112 105 112 101     */
0 0
原创粉丝点击