应该如何关闭InputStream和OutputStream?

发布于 2024-10-01 12:38:07 字数 452 浏览 0 评论 0原文

我使用以下代码来关闭与服务器的连接的输入流和输出流:

try {
        if (mInputStream != null) {
            mInputStream.close();
            mInputStream = null;
        }

        if (mOutputStream != null) {
            mOutputStream.close();
            mOutputStream = null;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

但是,流并未关闭,它们仍然存在。如果我再次连接,则会有两个不同的输入流。 catch 部分没有捕获任何异常。

我做错了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吹梦到西洲 2024-10-08 12:38:07

编辑:在底部添加了 Java 8 try-with-resources 示例,因为该语言自最初发布以来已经不断发展。

如果您使用的是 Java 7(或更低版本),则您发布的代码存在两个问题:

  1. .close() 调用应在 finally 块中处理。这样它们将始终被关闭,即使它在途中的某个地方落入了 catch 块。
  2. 您需要在其自己的 try/catch 块中处理每个 .close() 调用,否则您可能会使其中一个处于打开状态。如果您尝试关闭输入流失败,您将跳过关闭输出流的尝试。

您想要的更像是这样的:

    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
            }
        }
    }

如果您使用的是 Java 8+,则不需要任何 catch/finally 噪音。您可以使用 try-with-resources 语法,无论您何时以何种方式离开该块,Java 都会为您关闭资源:

    try(InputStream mInputStream = new FileInputStream("\\Path\\MyFileName1.txt")) {
        try(OutputStream mOutputStream = new FileOutputStream("\\Path\\MyFileName2.txt")) {
        //... do stuff to your streams
    }
}

Edit: Added the Java 8 try-with-resources example at the bottom, since the language has evolved since originally posted.

If you are using Java 7 (or lower), there are two problems with your posted code:

  1. 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.
  2. 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
            }
        }
    }

If you are using Java 8+, you don't need any of the catch/finally noise. You can use the try-with-resources syntax and Java will close the resource for you whenever and however you leave the block:

    try(InputStream mInputStream = new FileInputStream("\\Path\\MyFileName1.txt")) {
        try(OutputStream mOutputStream = new FileOutputStream("\\Path\\MyFileName2.txt")) {
        //... do stuff to your streams
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文