Java:Closing Streams:即使在close()之后Streams也是非NULL的

gen*_* b. 0 java

在我的finally子句中,我清理任何流,例如,

            finally // Clean up 
            {
                if (os != null) {
                    try {
                        os.close();
                    }
                    catch (IOException ioe) {
                        logger.warn("Failed to close outputStream", ioe);
                    }
                }
                if (is != null) {
                    try {
                        is.close();
                    }
                    catch (IOException ioe) {
                        logger.warn("Failed to close inputStream", ioe);
                    }
                }
Run Code Online (Sandbox Code Playgroud)

但是我看到即使关闭后Streams仍然是非NULL.那么检查NULL是错误的吗?或者我没有看到结果close

Fly*_*Boy 5

流"对象"是对流实例的引用.流是否开放是其州的一部分.close函数是一个在对象状态下运行的函数,因此不会影响对它的引用.

在将其设置为null之前,引用将保持非NULL,但是流的状态已关闭,这意味着您无法再使用它.

  • @geneb.根据这些帖子:http://stackoverflow.com/questions/8655901/how-to-check-whether-an-outputstream-is-closed和http://stackoverflow.com/questions/981196/how-to-know -if-a-bufferedreader-stream-is-closed你将无法看到......但是close函数永远不会抛出异常.你可以说:关闭后"os = null; is = null"使引用为null.但如果函数在关闭后完成,则不需要.你的代码很好;) (2认同)