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

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


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日

悬赏问题

  • ¥15 想编写一个期货跨期套利的程序
  • ¥15 UltraScale 系列 Bitslip 技术支持
  • ¥15 一个线程在sleep的时候set一个信号会起作用吗
  • ¥100 需求高精度PT100设计电路和算法
  • ¥15 单片机配网,继电器开关,广播
  • ¥60 Qcustomplot绘制实时动态曲线
  • ¥20 运用matlab画x-y图
  • ¥15 用idea运行项目,运行tomcat报错:断言失败
  • ¥15 Sqlserver查询链接服务器数据问题
  • ¥15 Bibtex4Word 引用中文文献