4

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?

In addition to that, does bufferedWriter.close() function set bufferedWriter to null?

sidgate
  • 14,650
  • 11
  • 68
  • 119
  • 1
    `if (writer ! = null)` then close it. – SatyaTNV Mar 30 '16 at 06:34
  • "does bufferedWriter.close() function set bufferedWriter to null" No, this is impossible. `bufferedWriter` is just a reference to a `BufferedWriter` instance; that instance can have zero or more references to it, and it has no way to know what actually does refer to it. – Andy Turner Mar 30 '16 at 07:00

4 Answers4

5

Calling close method on already closed Writer has no impact.

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.

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

sidgate
  • 14,650
  • 11
  • 68
  • 119
0

you can check if bufferredWriter not equal to null

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

If you are using java7 or more then you need not to worry about closing the BufferredWriter

JDK 7 onwards you can make you of try with resource

for example

try(BufferredWriter bufferedWriter=new BufferredWriter())
{

//your code
}
catch(Exception e)
{
}
SpringLearner
  • 13,738
  • 20
  • 78
  • 116
0
    BufferedWriter vf = new BufferedWriter(new FileWriter("file"));

    if (vf != null)
    {
        vf.close();
        vf.close(); //won't cause any problem
    }

you can close BufferedWriter as many times as you want if it is not null. So no need to check specifically if BufferedWriter is open or not.

Even better if you surround close statement in try/catch in case IOException occurs.

From javadocs

Closes the stream, flushing it first. Once the stream has been closed, further write() or flush() invocations will cause an IOException to be thrown. Closing a previously closed stream has no effect.

And as explained by sidgate, closing a stream won't nullify the reference you have to assign it manually.

codingenious
  • 8,385
  • 12
  • 60
  • 90
0

bufferedWriter.close() - Closes this writer. The contents of the buffer are flushed, the target writer is closed, and the buffer is released. Only the first invocation of close has any effect.

Refer this

Also, you can check like this

Define below 2 variables as instance variable

BufferedWriter bufferWriter;
boolean isOpen = false;

Then,

try {
    if (!isOpen) {
        bufferWriter = new BufferedWriter(new FileWriter(file, true));
        bufferWriter.write(initialData);
        isOpen = true;
    }
    bufferWriter.write(remainingData);
    bufferWriter.flush();
    Log.d(TAG, "written to file:" + file.getAbsolutePath());
} catch (IOException e) {
    Log.v("IOException", e.toString());
}
Raghu Nagaraju
  • 3,278
  • 1
  • 18
  • 25