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

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


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日

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统