I'm using the following code to close an InputStream and an OutputStream from a connection to a server:
try {
if (mInputStream != null) {
mInputStream.close();
mInputStream = null;
}
if (mOutputStream != null) {
mOutputStream.close();
mOutputStream = null;
}
} catch (IOException e) {
e.printStackTrace();
}
However, the streams are not closing, they are still alive. If I connect again there are two different InputStreams. No exception is being caught in the catch
section.
What am I doing wrong?
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
}
}
}
来源:https://stackoverflow.com/questions/4108236/how-should-an-inputstream-and-an-outputstream-be-closed