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

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:0
Now the content of the out: 
Now the byte size of buffer:1
Now the content of the out: a
Now the byte size of buffer:0
Now 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 1
s4's info: zdd2 2
Note:
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 
    */
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值