Java-IO之超类OutputStream

图中我们可以看出,以字节为单位的输出流的公共父类是OutputStream:


从中我们可以看出,以字节为单位的输出流的公共父类是OutputStream:
(1)OutputStream是以字节为单位的输出流的超类,提供了write()函数从输出流中读取字节数据。
(2)ByteArrayOutputStream是字节数组输出流,写入ByteArrayOutputStream的数据被写入到一个byte数组,缓冲区会随着数据的不断写入而自动增长,可使用toByteArray()和toString()获取数据。
(3)PipedOutputStream是管道输出流,和PipedInputStream一起使用,能实现多线程间的管道通信。
(4)FilterOutputStream是过滤输出流,是DataOutputStream,BufferedOutputStream和PrintStream的超类
(5)DataOutputStream是数据输出流,用来装饰其他的输出流,允许应用程序以与机器无关方式向底层写入基本Java数据类型。
(6)BufferedOutputStream是缓冲输出流,它的作用是为另一个输出流添加缓冲功能。
(7)PrintStream是打印输出流,用来装饰其他输出流,为其他输出流添加功能,方便的打印各种数据值
(8)FileOutputStream是文件输出流,通常用于向文件进行写入操作。
(9)ObjectOutputStream是对象输出流,它和ObjectInputStream一起对基本数据或者对象的持久存储。

基于JDK8的源代码分析:

public abstract class OutputStream implements Closeable, Flushable {  
    //写特定的字节到输出流  
    public abstract void write(int b) throws IOException;  
    //写特定的长度的字节数组到输出流中  
    public void write(byte b[]) throws IOException {  
        write(b, 0, b.length);  
    }  
    //从b数组中,起始值为off,长度为len的数据到输出流中  
    public void write(byte b[], int off, int len) throws IOException {  
        if (b == null) {  
            throw new NullPointerException();  
        } else if ((off < 0) || (off > b.length) || (len < 0) ||  
                ((off + len) > b.length) || ((off + len) < 0)) {  
                throw new IndexOutOfBoundsException();  
        } else if (len == 0) {  
            return;  
        }  
        for (int i = 0 ; i < len ; i++) {  
            write(b[off + i]);  
        }  
    }  
    //刷新输出流,强制任意的缓冲输出都被输出  
    public void flush() throws IOException {  
    }  
    //关闭输出流,释放与这个输出流相关的所有系统资源  
    public void close() throws IOException {  
    }  
}  


转载:https://blog.csdn.net/qq924862077/article/details/52689883


猜你喜欢

转载自blog.csdn.net/oqkdws/article/details/80255370
今日推荐