关闭流/套接字和try-catch-finally [英] Closing streams/sockets and try-catch-finally

查看:454
本文介绍了关闭流/套接字和try-catch-finally的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个例子我有问题(它来自另一个SO问题):

Here is an example I have questions about (it comes from another SO question):

public static void writeToFile (final String filename)
{
    PrintWriter out = null;     
    FileOutputStream fos = null;

    try
    {
        fos = new FileOutputStream(filename);
        out = new PrintWriter(new BufferedWriter(
                              new OutputStreamWriter(fos, "UTF-8")));

        for (final String line : log)
        {
            out.println(line);
        }

        out.flush();
        out.close();
    }
    catch (final Exception e)
    {
        System.err.println("Unable to write log to file.");
    }
    finally
    {
        if (fos != null)
        {
            try
            {
                fos.close();
            }
            catch (final IOException e)
            {
                System.err.println("Unable to write log to file.");
            }
        }
    }
}



我认为这个代码工作正常,并释放所有的资源,它应该等等。我的问题是:

Now I think this code works fine and releases all the resources where it should etc. My questions are:


  1. try-catch-finally 最后部分中的 FileOutputStream c>?当然,我可以把代码依次放在 try-catch

  1. Why should I be closing the FileOutputStream in the finally section of the try-catch-finally? Surely I could just put the code sequentially after the try-catch?

为什么必须关闭 FileOutputStream ,而不是简单地替换 new OutputStreamWriter(fos,... 新的FileOutputStream(filename),... ?如果我先关闭FileOutputStream,会自动关闭其余和释放资源吗?同样的问题适用于套接字,如果我关闭一个套接字连接自动关闭流读取器/写入器并释放资源?

Why must I close the FileOutputStream separately rather than simply replacing new OutputStreamWriter(fos, ... with new OutputStreamWriter(new FileOutputStream(filename), ...? If I close the FileOutputStream first, will that automatically close the rest and release the resources? Same questions apply for sockets, if I close a socket connection does that automatically close the stream readers/writers and release the resources?

我已经反复告知,以确保我用UTF-到不同的系统具有不同的字符集(或某事沿着这些线)。这是仍然适用于读/写RAW字节数据(例如从非文本文件,或加密的结果),因为我认为charsets在哪里做文本只有字符?

I've been told repeatedly to ensure that I read and write streams with "UTF-8" due to different systems having different charsets (or something along those lines). Is this still applicable when reading/writing RAW byte data (say from a non-text file, or the result of an encryption) as I thought charsets where to do with textual characters only?


推荐答案


  1. 因为如果你的代码抛出一个未处理的异常,try-catch bloc之后的代码段永远不会被执行。例如 NullPointerExcpetion 就是这种情况。

如果关闭信息流,其任何封闭的信息流也会关闭。

You don't have to. If you close a stream, any of its enclosed streams will be closed as well.

否。这是只有当字节转换为字符(或相反)。您要将字符集指定到 OutputStreamWriter ,它负责将字符转换为字节。

No. This is the case only when converting bytes to characters (or the other way around). You're specifying the charset to the OutputStreamWriter, which is responsible for converting characters to bytes.

这篇关于关闭流/套接字和try-catch-finally的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆