ByteArrayOutputStream或ByteArrayInputStream不需要关闭流原因分析

转自:https://blog.csdn.net/u012668925/article/details/53941471 

在解压图片的时候发现ByteArrayOutputStream不需要关闭,为啥呢? 
ByteArrayOutputStream或ByteArrayInputStream是内存读写流,不同于指向硬盘的流,它内部是使用字节数组读内存的,这个字节数组是它的成员变量,当这个数组不再使用变成垃圾的时候,Java的垃圾回收机制会将它回收。所以不需要关流。如下图所示,ByteArrayOutputStream内部是数组: 
 
然后再看看FileOutputStream的构造: 

è¿éåå¾çæè¿°

很明显FileOutputStream的构造方法打开了IoBridge, 
也就是说,指向内存的流可以不用关闭,指向存储卡/硬盘的流一定要关闭。

通过ByteArrayInputStream和ByteArrayOutputStream的源码close()方法也可以发现,调用它们的close()方法啥也不会发生。

    /**
     * Closing a <tt>ByteArrayInputStream</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 {
    }
    /**
     * 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 {
    }

最后

不过我的建议是只要是流,用完后都在finally块里调用其close()方法进行关闭。万一哪天你记混了,认为ObjectOutputSteam不需要close,项目上生产环境出了问题,相信我,你会哭的!

  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是的,在上面的示例中,我们使用了 `ObjectOutputStream` 和 `ObjectInputStream` 对象来序列化和反序列化 `List<Long>`。在使用完这些对象后,我们应该及时关闭这些,以释放资源并避免可能的内存泄漏。 以下是一个修改后的示例代码,展示如何在正确关闭的情况下将 `List<Long>` 存储到 SQLite3 数据库中,并且读取 `List<Long>`: ```java // 存储 List<Long> List<Long> list = new ArrayList<>(); list.add(1L); list.add(2L); list.add(3L); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream out = null; try { out = new ObjectOutputStream(bos); out.writeObject(list); byte[] bytes = bos.toByteArray(); ContentValues values = new ContentValues(); values.put("mylist", bytes); long result = db.insert("mytable", null, values); } catch (IOException e) { e.printStackTrace(); } finally { if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } // 读取 List<Long> Cursor cursor = db.query("mytable", new String[]{"mylist"}, null, null, null, null, null); if (cursor.moveToFirst()) { byte[] bytes = cursor.getBlob(cursor.getColumnIndex("mylist")); ByteArrayInputStream bis = new ByteArrayInputStream(bytes); ObjectInputStream in = null; try { in = new ObjectInputStream(bis); List<Long> resultList = (List<Long>) in.readObject(); // resultList 就是读取到的 List<Long> } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } ``` 在这个修改后的示例中,我们在 `try-catch` 块中创建了 `ObjectOutputStream` 和 `ObjectInputStream` 对象,并在 `finally` 块中关闭了这些,以确保它们在使用完毕后会被正确关闭

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值