Formatter 如何在不关闭的情况下与 BufferedWriter 一起工作?

How Formatter works with BufferedWriter without closing?

在学习 Java 的过程中,我发现(并在我的代码中匆忙使用了)来自 JDK 的以下代码模式:

public Formatter(OutputStream os, String csn, Locale l)
    throws UnsupportedEncodingException
{
    this(l, new BufferedWriter(new OutputStreamWriter(os, csn)));
}

它将新创建的 BufferedWriter 传递给 other constructor,将其称为 Appendable 接口(这样它就无法在 Formatter 中进一步刷新或关闭实施):

private Formatter(Locale l, Appendable a) {
    this.a = a;
    this.l = l;
    this.zero = getZero(l);
}

经过一番思考并挖掘出 BufferedWriter 代码后,我不明白这是如何正常工作的 - 从 BufferedWriter 代码看来,如果完成后不关闭,它将丢失缓冲数据。 Finalize 方法好像没有用。 Formatter class 中还有许多其他构造函数以类似的方式使用 BufferedWriter(主要是包装输出流和文件)。那么缓冲区刷新在这段代码中是如何工作的呢?或者这是 JDK 中的一堆错误(这听起来令人难以置信,因为它是非常基本和旧的功能)?

Formatter 是一个 Closeable。就像输出流一样,它因此应该由格式化程序的用户关闭,并且关闭它会关闭支持 BufferedWriter(和包装的 OutputStream,可传递):

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/util/Formatter.java#Formatter.close%28%29

因此,如果 Formatter 是使用 OutputStream 构建的,则客户端代码有责任关闭它。