写法 1:在 try 中关流,而没在 finally 中关流
try {
OutputStream out = new FileOutputStream("file");
// ...操作流代码
out.close();
} catch (Exception e) {
e.printStackTrace();
}
当操作流代码报错的时候,这种写法会导致流无法正常的关闭,因此不推荐采用!
正确的操作方式,应该在finally
里面完成,实例代码如下:
OutputStream out = null;
try {
out = new FileOutputStream("file");
// ...操作流代码
} catch (Exception e) {
e.printStackTrace();
} finally {
// 在 finally 中进行关闭,确保一定能被执行
try {
if (out != null) {
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
写法 2:在关闭多个流时,将其放在一个 try 中
在关闭多个流时,有的同学嫌弃麻烦,将其放在一个 try 中完成,实例代码如下:
OutputStream out1 = null;
OutputStream out2 = null;
try {
out1 = new FileOutputStream("file");
out2 = new FileOutputStream("file");
// ...操作流代码
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out1 != null) {
// 如果此处出现异常,则out2流没有被关闭
out1.close();
}
if (out2 != null) {
out2.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
这种写法下,当out1.close
出异常的时候,out2.close
是不会被正常关闭的,因此不推荐采用!
正确的操作方式,应该是一个一个的close
,别偷懒,实例代码如下:
OutputStream out1 = null;
OutputStream out2 = null;
try {
out1 = new FileOutputStream("file");
out2 = new FileOutputStream("file");
// ...操作流代码
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out1 != null) {
out1.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (out2 != null) {
out2.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
写法 3:在循环中创建流,在循环外关闭
有的同学在循环操作多个文件时,在循环外关闭文件流,实例代码如下:
OutputStream out = null;
try {
for (int i = 0; i < 10; i++) {
out = new FileOutputStream("file");
// ...操作流代码
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
表面看上去好像没有问题,但是实际上创建了 10 个 IO 流,try 里面的逻辑执行完成之后,只是把最后的一个 IO 流对象赋予给了out
参数。也就是当程序执行完毕之后,只关闭了最后一个 IO 流,其它 9 个 IO 流没用被手动关闭,因此不推荐采用!
正确的操作方式,应该是在循环体内close
,别偷懒,实例代码如下:
for (int i = 0; i < 10; i++) {
OutputStream out = null;
try {
out = new FileOutputStream("file");
// ...操作流代码
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
写法 4:关闭多个流时,没用遵循后定义先释放原则
有的同学在操作多个文件流时,操作完成之后,依照先后次序进行关闭文件流,实例代码如下:
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
fos = new FileOutputStream("file");
bos = new BufferedOutputStream(fos);
// ...操作流代码
} catch (Exception e){
} finally {
// 依次关闭流
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
// 此处会报 java.io.IOException: Stream Closed 错误
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
按照先后顺序关闭文件流,这种写法下,有可能会报java.io.IOException: Stream Closed
错误。
原因是BufferedOutputStream
依赖于FileOutputStream
,如果直接关闭FileOutputStream
流,再次关闭BufferedOutputStream
,会提示源头已经被关闭,缓存区数据无法输出。
正确的操作方式,应该遵循后定义先释放的原则,实例代码如下:
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
fos = new FileOutputStream("file");
bos = new BufferedOutputStream(fos);
// ...操作流代码
} catch (Exception e){
} finally {
// 后定义先释放
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
写法 5:jdk7 及以上版本,推荐采用 try-with-resources 写法
try-with-resources
是 JDK 7 中引入的一个新的异常处理机制,它能让开发人员不用显式的释放try-catch
语句块中使用的资源。
以上文为例,可以改成如下写法:
try (FileOutputStream fos = new FileOutputStream("file");
BufferedOutputStream bos = new BufferedOutputStream(fos)){
// ...操作流代码
} catch (Exception e){
e.printStackTrace();
}
try-with-resources
释放资源的操作,也是遵循的后定义先释放的原则!
写法 6:使用包装流时,只需要关闭最后面的包装流即可
包装流是指通过装饰设计模式实现的 IO 流类,其目的是对底层流的功能进行扩展,在实际数据传输的时候,还是使用底层流进行传输。比如缓存字节输出流BufferedOutputStream
就是一个包装流,目的是对字节输出流提供一个缓存区功能,让数据输出效率更高。
在使用到包装流的时候,我们只需要关闭最后面的包装流即可。
以上文为例,改写的实例代码如下:
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
is = new FileInputStream("file");
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
// ...操作流代码
} catch (Exception e){
e.printStackTrace();
} finally {
// 关闭包装流,也会自动关闭 InputStream 流
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
总结:
在上文中,我们提到只要是 IO 流都建议大家手机关闭资源,但是在 Java 中有一种流,它是不需要手动关闭的,比如内存读写流:ByteArrayInputStream
、ByteArrayOutputStream
。
不同于指向硬盘的流,ByteArrayInputStream
和ByteArrayOutputStream
其实是伪装成流的字节数组存储在内存中(把它们当成字节数据来看就好了),他们不会锁定任何文件句柄和端口,如果不再被使用,字节数组会被垃圾回收掉,所以不需要关闭。
当 IO 流是指向存储卡 / 硬盘 / 网络等外部资源的流,是一定要手动关闭的。