 <p>you can check if <code>bufferredWriter</code> not equal to null</p> <pre><code>if(bufferredWriter!=null) { bufferredWriter.close(); } </code></pre> <p>If you are using java7 or more then you need not to worry about closing the BufferredWriter </p> <p>JDK 7 onwards you can make you of try with resource </p> <p>for example</p> <pre><code>try(BufferredWriter bufferedWriter=new BufferredWriter()) { //your code } catch(Exception e) { } </code></pre>