秋日的晚霞 2021-08-13 16:09 采纳率: 94.4%
浏览 112
已结题

关闭字节缓冲流还需要关闭字节流吗


public void download() throws MyException, IOException {
        ClientGlobal.init("D:\\project_work_changgou\\changgou-service\\changgou-service-file\\src\\main\\resources\\fdfs_client.conf");

        TrackerServer trackerServer = new TrackerClient().getConnection();

        StorageClient storageClient = new StorageClient(trackerServer,null);


        byte[] group1s = storageClient.download_file("group1", "M00/00/00/wKjThGEWlNqAY3G9AASGXSYJjlQ243.jpg");

        FileOutputStream fileOutputStream = new FileOutputStream(new File("D://abcd.jpg"));

        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

        try {

            bufferedOutputStream.write(group1s);

            bufferedOutputStream.flush();

        } finally {
            bufferedOutputStream.close();
        }

    }

关闭字节缓冲流还需要关闭字节流吗

  • 写回答

3条回答 默认 最新

  • huoxin4415 2021-08-13 16:40
    关注

    去看了一下源码,BufferedOutputStream构造方法如下:

    public class BufferedOutputStream extends FilterOutputStream {
        /**
         * The internal buffer where data is stored.
         */
        protected byte buf[];
    
        /**
         * The number of valid bytes in the buffer. This value is always
         * in the range <tt>0</tt> through <tt>buf.length</tt>; elements
         * <tt>buf[0]</tt> through <tt>buf[count-1]</tt> contain valid
         * byte data.
         */
        protected int count;
    
        /**
         * Creates a new buffered output stream to write data to the
         * specified underlying output stream.
         *
         * @param   out   the underlying output stream.
         */
        public BufferedOutputStream(OutputStream out) {
            this(out, 8192);
        }
    
        /**
         * Creates a new buffered output stream to write data to the
         * specified underlying output stream with the specified buffer
         * size.
         *
         * @param   out    the underlying output stream.
         * @param   size   the buffer size.
         * @exception IllegalArgumentException if size &lt;= 0.
         */
        public BufferedOutputStream(OutputStream out, int size) {
            super(out);
            if (size <= 0) {
                throw new IllegalArgumentException("Buffer size <= 0");
            }
            buf = new byte[size];
        }

    可以看出BufferedOutputStream继承FilterOutputStream,BufferedOutputStream构造方法调用了super(out),其源码如下:

    public class FilterOutputStream extends OutputStream {
        /**
         * The underlying output stream to be filtered.
         */
        protected OutputStream out;
    
        /**
         * Creates an output stream filter built on top of the specified
         * underlying output stream.
         *
         * @param   out   the underlying output stream to be assigned to
         *                the field <tt>this.out</tt> for later use, or
         *                <code>null</code> if this instance is to be
         *                created without an underlying stream.
         */
        public FilterOutputStream(OutputStream out) {
            this.out = out;
        }
    
        /**
         * Closes this output stream and releases any system resources
         * associated with the stream.
         * <p>
         * The <code>close</code> method of <code>FilterOutputStream</code>
         * calls its <code>flush</code> method, and then calls the
         * <code>close</code> method of its underlying output stream.
         *
         * @exception  IOException  if an I/O error occurs.
         * @see        java.io.FilterOutputStream#flush()
         * @see        java.io.FilterOutputStream#out
         */
        @SuppressWarnings("try")
        public void close() throws IOException {
            try (OutputStream ostream = out) {
                flush();
            }
        }

    因BufferedOutputStream继承FilterOutputStream,且并未重写close方法,所以在BufferedOutputStream调用close方法时,会调用父类FilterOutputStream的close方法。从该方法可以看出,将构造时的OutputStream(也就是上面的FileOutputStream)包裹在了try语句扩括号内,针对jdk1.8的特性,会对实现了Closeable接口的类自动调用close方法。也就是说,不需要再手动关闭字节流,BufferedOutputStream会在close的时候,关闭构造它的FileOutputStream。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 8月21日
  • 已采纳回答 8月13日
  • 创建了问题 8月13日

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格