In Android, How to close InputStream and OutputStream?

Two problems with your posted code: The .close() calls should be handled in a finally block. This way they will ALWAYS be closed, even if it fell into a catch block somewhere along the way You need to handle EACH .close() call in its own try/catch block or you could leave one of them stranded open. If your attempt to close the input stream failed you would be skipping over the attempt to close the output stream You want something more like this: InputStream mInputStream = null; OutputStream mOutputStream = null; try { mInputStream = new FileInputStream("\\Path\\MyFileName1.

Txt"); mOutputStream = new FileOutputStream("\\Path\\MyFileName2. Txt"); //... do stuff to your streams } catch(FileNotFoundException fnex) { //Handle the error... but the streams are still open! } finally { //close input if (mInputStream!

= null) { try { mInputStream.close(); } catch(IOException ioex) { //Very bad things just happened... handle it } } //Close output if (mOutputStream! = null) { try { mOutputStream.close(); } catch(IOException ioex) { //Very bad things just happened... handle it } } }.

Two problems with your posted code: The .close() calls should be handled in a finally block. This way they will ALWAYS be closed, even if it fell into a catch block somewhere along the way. You need to handle EACH .close() call in its own try/catch block or you could leave one of them stranded open.

If your attempt to close the input stream failed you would be skipping over the attempt to close the output stream. You want something more like this: InputStream mInputStream = null; OutputStream mOutputStream = null; try { mInputStream = new FileInputStream("\\Path\\MyFileName1. Txt"); mOutputStream = new FileOutputStream("\\Path\\MyFileName2.

Txt"); //... do stuff to your streams } catch(FileNotFoundException fnex) { //Handle the error... but the streams are still open! } finally { //close input if (mInputStream! = null) { try { mInputStream.close(); } catch(IOException ioex) { //Very bad things just happened... handle it } } //Close output if (mOutputStream!

= null) { try { mOutputStream.close(); } catch(IOException ioex) { //Very bad things just happened... handle it } } }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions