Java BufferedWriter 和 OutputStream .close() 方法

Java BufferedWriter and OutputStream .close() method

我是 java 的新手,有一个关于 BufferedWriter 和 OutputStream 关​​闭的问题。

我有一些逻辑,不方便使用 try-with-resources:

public static void writeFile(String fileName, String encoding, String payload) {

    BufferedWriter writer = null;
    OutputStream stream = null;

    try {
        boolean needGzip = payload.getBytes(encoding).length > gzipZize;

        File output = needGzip ? new File(fileName + ".gz") : new File(fileName);
        stream = needGzip ? new GZIPOutputStream(new FileOutputStream(output)) : new FileOutputStream(output);

        writer = new BufferedWriter(new OutputStreamWriter(stream, encoding));
        writer.write(payload);

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            writer.close();
            stream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

所以,我必须自己关闭所有资源。我应该关闭 OutputStream 和 BufferedWriter 吗?或者只关闭 BufferedWriter 就可以了?

我的代码一切正常吗?

只需关闭 BufferedWriter. 如果您遵循 Javadoc,您会看到它关闭了所有嵌套流。

如果关闭 BufferedWriter,它的流也将关闭,但 BufferedWriter 和 OutputStream 都实现了 Closeable。因此,如果您愿意,可以使用 try with resource 来为您处理关闭

例如:

public static void writeFile(String fileName, String encoding, String payload) {
    File output = new File(fileName);
    try (OutputStream stream = new FileOutputStream(output);
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stream, encoding))) {
        writer.write(payload);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

编辑: 添加了 getStream 以检查是否需要 gzip 流

注意:这个答案只是你代码的一个"update",我不确定你一般想做什么,所以它可能不是适合您程序的最佳解决方案

public static void writeFile(String fileName, String encoding, String payload) {
    try (BufferedWriter writer = new BufferedWriter(
            new OutputStreamWriter(getStream(fileName, encoding, payload), encoding))) {
        writer.write(payload);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static OutputStream getStream(String fileName, String encoding, String payload) throws IOException {
    boolean needGzip = payload.getBytes(encoding).length > gzipZize;
    File output = needGzip ? new File(fileName + ".gz") : new File(fileName);
    return needGzip ? new GZIPOutputStream(new FileOutputStream(output)) : new FileOutputStream(output);
}

不行,交给Java,让它处理:

public static void writeFile(String fileName, String encoding,
            String payload) {
        boolean needGzip = payload.getBytes(Charset.forName(encoding)).length > gzipZize;
        File output = needGzip ? new File(fileName + ".gz")
                : new File(fileName);
        try (OutputStream stream = needGzip ? new GZIPOutputStream(
                new FileOutputStream(output)) : new FileOutputStream(output);
                BufferedWriter writer = new BufferedWriter(
                        new OutputStreamWriter(stream, encoding))) {
            writer.write(payload);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }