Java IO包 之 OutputStream

440 阅读1分钟

Java.io包中最重要的是5个类,分别是:

  • File
  • OutputStream
  • InputStream
  • Writer
  • Reader

最重要的一个接口:

  • Serializable

这里介绍的是 OutputStream

OutputStream

OutputStream 源码如下:

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);
    }
    
    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 {
    }
}