zoukankan      html  css  js  c++  java
  • Java中的InputStream和OutputStream

    Java中的InputStream和OutputStream

    做Java技术好几个月了,碰到了很多琐碎细小的技术点,一直没有记录。“勿以善小而不为”,正好今天有点空,先从两个Stream说起。

    InputStream vs OutputStream

    InputStream用来进行数据读取,而OutputStream用来进行数据写入。接口描述:

    public abstract class InputStream implements Closeable {
        public abstract int read() throws IOException;
        public int read(byte b[]) throws IOException;
        public int read(byte b[], int off, int len) throws IOException;
        public long skip(long n) throws IOException;
        public void close() throws IOException;
        public synchronized void mark(int readlimit);
        public synchronized void reset() throws IOException;
        //...
    }
    
    public abstract class OutputStream implements Closeable, Flushable {
       public abstract void write(int b) throws IOException;
       public void write(byte b[]) throws IOException;
       public void write(byte b[], int off, int len) throws IOException;
       public void flush() throws IOException;
       public void close() throws IOException;
    }
    

     Close Streams

    Java中的Stream是需要通过close方法显式进行关闭的。OutputStream上面还有一个方法叫flush,close方法会自动执行flush。所以在调用close之前无需调用flush方法。

    如果一个Stream在构建的时候,通过构造函数传入另外一个Stream,当该Stream被close的时候,原始的Stream也会被关闭,参见如下代码:

    OutputStream file = new FileOutputStream( "quarks.ser" );
    OutputStream buffer = new BufferedOutputStream( file );
    ObjectOutput output = new ObjectOutputStream( buffer );
    try{
            output.writeObject(quarks);
    }
    finally{
            output.close();
    }
    

     派生类

    inputstream

    outputstream

    有时间的话会把每个派生类简单解释一下。

  • 相关阅读:
    redis-3.2.5 make 报错
    haproxy 实现多域名证书https
    centos7修改主机名
    ngx_image_thumb模块生成缩略图
    查看nginx在安装时开启了哪些模块
    C# install-package:"xx"已拥有为“xxx”定义的依赖项
    JetBrains 2017/2018全系列产品激活工具
    查看win10版本方法,及win10升级方法
    Windows 10正式版的历史版本
    open '/dev/hwlog_switch' fail -1, 13. Permission denied
  • 原文地址:https://www.cnblogs.com/tedzhao/p/Java_InputStream_OutputStream.html
Copyright © 2011-2022 走看看