当我将一个InputStream传递给这个方法时,它会关闭它吗?
public void foo(InputStream is) {
DataInputStream dis = new DataInputStream(is);
dis.close();
}超类FilterInputStream也将close方法重新定义为关闭基础输入流,因此它将关闭参数is。
该操作会影响调用方的输入流吗?
发布于 2016-03-02 08:46:15
是的,传入的流将被关闭。
关闭您没有打开的流几乎不是一个好主意,因此您不应该在此方法中关闭dis。
DataInputStream没有自己的系统资源,所以不关闭它不会导致任何泄漏。你可以直接把它打开。或者,您可以从方法中返回它,以便调用方可以关闭它。
发布于 2016-03-02 08:54:04
是的,DataInputStream是一个经过InputStream is的装饰类.它不是InputStream,它是自我。
因此,从本质上说,DataInputStream.close意味着底层InputStream上的close。
/** * Closes this input stream and releases any system resources * associated with the stream. * This * method simply performs <code>in.close()</code>. * * @exception IOException if an I/O error occurs. * @see java.io.FilterInputStream#in */ public void close() throws IOException { in.close(); }
发布于 2016-03-02 08:47:33
InputStream将关闭。
由于Java 7,最好是使用尝试-与-资源,以便流将自动关闭。
https://stackoverflow.com/questions/35742127
复制相似问题