我该如何检查BufferedWriter是否已经关闭? [英] How should I check if BufferedWriter is already closed?

查看:238
本文介绍了我该如何检查BufferedWriter是否已经关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在android中,我正在编写一个单击按钮的文件,并在下次单击时,它保存文件并关闭缓冲的编写器。但是,我还希望实现在onDestroy函数中关闭缓冲编写器的功能。在此之前,我需要知道 Bufferedwriter 是否已经关闭。我如何检查Buffered Writer是否已经关闭?

In android, I am writing a file on clicking a button and on clicking next time, it saves the file and closes the buffered writer. But, I also want to implement functionality to close the buffered writer in onDestroy function. Before that I need to know if Bufferedwriter is already closed. How will I check if Buffered Writer is already closed?

除此之外, bufferedWriter.close()函数集 bufferedWriter null

推荐答案

在已关闭的Writer上调用 close 方法没有任何影响。

Calling close method on already closed Writer has no impact.

但是,如果你想知道Writer是否已关闭,你可以调用 writer.flush(),如果它抛出 IOException 那么这意味着Writer已经关闭。

Still, if you want to know if the Writer is closed, you can call writer.flush(), if it throws IOException then it means the Writer is already closed.

对于你的第二个问题,关闭一个流没有't无效参考。您必须将其显式设置为 null

For your second question, closing a stream doesn't nullify the reference. You have to explicitly set it to null.

这篇关于我该如何检查BufferedWriter是否已经关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆