Java基础系列:I/O流关闭解析(源码)

1 流

序号方法描述
1write写入数据
2flush刷新内存数据
3close关闭流

2 流关闭

FileOutputStream是接收数据的管道,使用结束,需要进行关闭,如果不进行关闭,可以一直向这个管道写入数据,会导致OOM,若出现异常,JVM不能正常回收该对象,内存泄露,可能最终导致OOM。
使用流之后,必须关闭
关闭流有两种方式:

  • try…catch…finally在finally里统一关闭
  • Java7及之后,在try(io)中自动关闭
    I/O流源码介绍如下:

2.1 FileOutputStream

FileOutputStream是常用的数据接收通道,常用的方法有写(write)、读(read)、关闭(close),其中关闭方法继承OutputStream,因此FileOutputStream使用的close为OutputStream的方法,在close方法中调用了channel类,该类在AbstractInterruptibleChannel类中实现。

public
class FileOutputStream extends OutputStream
{
/**
     * Closes this file output stream and releases any system resources
     * associated with this stream. This file output stream may no longer
     * be used for writing bytes.
     *
     * <p> If this stream has an associated channel then the channel is closed
     * as well.
     *
     * @exception  IOException  if an I/O error occurs.
     *
     * @revised 1.4
     * @spec JSR-51
     */
    public void close() throws IOException {
        synchronized (closeLock) {
            if (closed) {
                return;
            }
            closed = true;
        }

        if (channel != null) {
            channel.close();
        }

        fd.closeAll(new Closeable() {
            public void close() throws IOException {
               close0();
           }
        });
    }
}

2.2 OutputStream

OutputStream为抽象类,关闭方法源码如下:

package java.io;


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

}

2.3 FileChannel

public abstract class FileChannel
    extends AbstractInterruptibleChannel
    implements SeekableByteChannel, GatheringByteChannel, ScatteringByteChannel
{

2.4 AbstractInterruptibleChannel

public abstract class AbstractInterruptibleChannel
    implements Channel, InterruptibleChannel
{
public final void close() throws IOException {
        synchronized (closeLock) {
            if (!open)
                return;
            open = false;
            implCloseChannel();
        }
    }
}

2.4 ObjectOutputStream

关闭close调用了flush和clear。

public class ObjectOutputStream
    extends OutputStream implements ObjectOutput, ObjectStreamConstants
{

/**
     * Closes the stream. This method must be called to release any resources
     * associated with the stream.
     *
     * @throws  IOException If an I/O error has occurred.
     */
    public void close() throws IOException {
        flush();
        clear();
        bout.close();
    }
}

3 小结

  • I/O用完必须关闭,如果不进行关闭,其一,可以一直向这个管道写入数据,会导致OOM,若出现异常,JVM不能正常回收该对象,内存泄露,可能最终导致OOM;其二,不关闭,并且未调用flush刷新内存方法,会丢失数据,缓存的空间为8192字节,若数据量不是8192字节的N倍,不会自动刷新,导致,小于8192字节的数据无法刷新到缓存中,丢失数据
  • close()方法分别调用了flush和clear,即使用
  • flush刷新内存,保证数据完全写入,clear释放内存

【参考文献】
[1]https://blog.csdn.net/u011541946/article/details/81150610
[2]https://blog.csdn.net/u012383839/article/details/88625262
[3]https://www.cnblogs.com/Cuimc/archive/2004/01/13/10755192.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天然玩家

坚持才能做到极致

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值