该类是输出流的“鼻祖”,所有的输出流都继承了该类
该类完成了Closeable,Flushable接口
无引入其他包
该类的类头注释如下:
/** * This abstract class is the superclass of all classes representing * an output stream of bytes. An output stream accepts output bytes * and sends them to some sink. * <p> * Applications that need to define a subclass of * <code>OutputStream</code> must always provide at least a method * that writes one byte of output. * * @author Arthur van Hoff * @see java.io.BufferedOutputStream * @see java.io.ByteArrayOutputStream * @see java.io.DataOutputStream * @see java.io.FilterOutputStream * @see java.io.InputStream * @see java.io.OutputStream#write(int) * @since JDK1.0 */
大意如下:
该抽象类是所有byte输出流的顶级类
一个输出流接收需要输出的bytes然后发送到底层
应用需要去声明该类的子类,并且必须至少提供用于写出一个byte的方法
该类含有如下的成员方法:
写出一个byte(必须被子类提供)
public abstract void write(int b) throws IOException;
写出一个byte数组
public void write(byte b[]) throws IOException { write(b, 0, b.length); }
写出byte数组中的特定位置和长度的byte
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++) {//循环调用写出一个byte的方法 write(b[off + i]); } }
刷新流(该类会强制流中的缓存数据立即被写出,一般也是这么用,写了一些东西,差不多就flush。该方法只能保证将数据提交给操作系统,不能保证数据被写到物理介质上(当输出目标为操作系统提供的抽象概念时,如文件))
public void flush() throws IOException { }
关闭流
public void close() throws IOException { }
该类的结构基本就是所有输出类的大体结构,其他输出流在此基础上进行扩充,例如buffer类添加了缓冲区,writer提供了转码,Filter提供了双层输出流来进行过滤(内含实际的输出流,外部封包了一层)
Closeable该类继承自AutoCloseable类
引入了如下包(我也不知道为啥在同一个包内需要import,估计是写错了)
import java.io.IOException;
该类类头注释如下:
/** * A {@code Closeable} is a source or destination of data that can be closed. * The close method is invoked to release resources that the object is * holding (such as open files). * * @since 1.5 */
大意如下:
Closeable标识了可以被关闭数据源或者目标
close方法被调用来释放被该对象掌控的资源(例如打开的文件)
该类含有如下的成员方法:
关闭当前对象,释放资源(在流已经关闭的情况下不产生任何影响。该方法在某些特定情况下可能会调用失败,抛出IO异常。在这种情况下强烈建议先放弃底层资源,在IOException被抛出前内部关闭(特定情况见AutoCloseable))
public void close() throws IOException;
该类被几乎所有输入输出类继承,效果和作用也十分清晰。只是在报错的时候需要注意出现的是哪种特殊情况,同时并处理Close失败的异常。