解决方案 »

  1.   

    你看的哪个是空方法,肯定内部都有实现的~    /**
         * Closes this file input stream and releases any system resources
         * associated with the stream.
         *
         * <p> If this stream has an associated channel then the channel is closed
         * as well.
         *
         * @exception  IOException  if an I/O error occurs.
         *
         * @revised 1.4
         * @spec JSR-51
         */    public void close() throws IOException {
            if (channel != null)
                channel.close();
            close0();
        }
      

  2.   

       
      比如那个ByteArrayInputStream  ,为什么这个close方法是空的呢    
      

  3.   

    官方文档的介绍:
    public class ByteArrayInputStream
    extends InputStream
    A ByteArrayInputStream contains an internal buffer that contains bytes that may be read from the stream. An internal counter keeps track of the next byte to be supplied by the read method.
    Closing a ByteArrayInputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.
    应该是这个流比较特殊,红色的这句话说了,关闭它没有任何效果~
      

  4.   

    既然close是个空方法,那么调用他释放与流相关的系统资源是怎么实现的呢?例如 FileInputStream 是继承至 InputStream.
    InputStream is = new FileInputStream();
    is.close(); // 这时调用的是FileInputStream的close(),而不是InputStream的close(),因为is是FileInputStream的实例对象,这个就是多态。而FileInputStream里的close()是有实现的。
      

  5.   

    Basically,wlwlwlwl015 and Inhibitory have answered your questions, but it's not from the perspective of design.I guess you still feel confused about why the author designed like that. Let's say, why is it designed to be an empty implementation method rather than abstract method?