ByteArrayOutputStream或ByteArrayInputStream不需要关闭

在做excel导入,有个需求是发现填写错误,要高亮错误单元格并让用户重新下载。当时用公司的云存储服务保存错误excel,得到文件链接。

上传时先用ByteArrayOutputStream来暂存excel的流。编程有个好的意识是用完流要及时关闭,就思考ByteArrayOutputStream要不要关。

答案是关不关都行,ByteArrayOutputStream的close方法是空实现,ByteArrayOutputStream是基于内存的流,而不是指向硬盘或者网络,用完了就被GC清理掉。

	/**
     * Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in
     * this class can be called after the stream has been closed without
     * generating an <tt>IOException</tt>.
     */
    public void close() throws IOException {
    
    
    }

参考:https://blog.csdn.net/pange1991/article/details/86575967

猜你喜欢

转载自blog.csdn.net/qq_42747210/article/details/113063110