OutputStream分析

上篇我们写了字节输入流的父类:InputStream,这篇我们接着分析字节输出流的父类:OutputStream。它实现了Closeable, Flushable接口,上篇介绍了Closeable接口,这篇我们就介绍Flushable接口,Flushable接口定义如下:

public interface Flushable {

    //通过将任何缓冲的输出写入基础流来刷新此流
    void flush() throws IOException;
}

方法:

   write(int b)


//将指定的一个字节写入此输出流。int 值为 4 个字节,此方法丢弃 int 类型高位的 3 个字节,只保留低
//位的 1 个字节写入(对字节来说,转为 int 其 3 个高位字节都是全 0 的,所以可以丢弃)。此方法是抽
//象方法,子类必须要进行实现。
public abstract void write(int b) throws IOException;

write(byte b[], int off, int len) 


//将 b.length 个字节从指定的 byte 数组写入此输出流。调用 write(b, 0, b.length) 方法。
public void write(byte b[]) throws IOException {
        write(b, 0, b.length);
 }

write(byte b[], int off, int len) 

 
 //将指定 byte 数组中从偏移量 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) {
            // 如果写入长度为0,不需要处理
            return;
        }
        // 从off下标处开始写入每一个字节到输出流。
        for (int i = 0 ; i < len ; i++) {
            write(b[off + i]);
        }
 }

flush() 

 
//刷新此输出流并强制写出所有缓冲的输出字节。此类未实现具体行为,子类应该复写此方法。
public void flush() throws IOException {
  }

close() 


//关闭此输出流并释放与此流有关的所有系统资源,此类未实现具体行为,子类应该复写此方法。
public void close() throws IOException {
    }

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值