浅谈Java如何正确关闭流

浅谈Java如何正确关闭流

文章目录

1.在try中关流,而没在finally中关流2.在关闭多个流时因为嫌麻烦将所有关流的代码丢到一个try中3.在循环中创建流,在循环外关闭,导致关闭的是最后一个流4.在Java7中,关闭流这种繁琐的事情再也不用我们自己敲代码了5.内存流可以不用关闭(关与不关都可以,没影响)6.使用装饰流时,只需要关闭最后面的装饰流即可7.关闭流时的顺序问题8.深究为什么一定要关闭流的原因9.`推荐使用NIO的Files工具类替换FileInputStream和FileOutputStream`

1.在try中关流,而没在finally中关流

try {

OutputStream out = new FileOutputStream("");

// ...操作流代码

out.close();

} catch (Exception e) {

e.printStackTrace();

}

正确写法:

OutputStream out = null;

try {

out = new FileOutputStream("");

// ...操作流代码

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (out != null) {

out.close();

}

} catch (Exception e) {

e.printStackTrace();

}

}

2.在关闭多个流时因为嫌麻烦将所有关流的代码丢到一个try中

OutputStream out = null;

OutputStream out2 = null;

try {

out = new FileOutputStream("");

out2 = new FileOutputStream("");

// ...操作流代码

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (out != null) {

out.close();// 如果此处出现异常,则out2流没有被关闭

}

if (out2 != null) {

out2.close();

}

} catch (Exception e) {

e.printStackTrace();

}

}

正确写法:

OutputStream out = null;

OutputStream out2 = null;

try {

out = new FileOutputStream("");

out2 = new FileOutputStream("");

// ...操作流代码

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (out != null) {

out.close();// 如果此处出现异常,则out2流也会被关闭

}

} 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("");

// ...操作流代码

}

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (out != null) {

out.close();

}

} catch (Exception e) {

e.printStackTrace();

}

}

正确写法:

for (int i = 0; i < 10; i++) {

OutputStream out = null;

try {

out = new FileOutputStream("");

// ...操作流代码

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (out != null) {

out.close();

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

4.在Java7中,关闭流这种繁琐的事情再也不用我们自己敲代码了

try (OutputStream out = new FileOutputStream("")){

// ...操作流代码

} catch (Exception e) {

e.printStackTrace();

}

只要实现的自动关闭接口(Closeable)的类都可以在try结构体上定义,java会自动帮我们关闭,及时在发生异常的情况下也会。可以在try结构体上定义多个,用分号隔开即可,如:

try (OutputStream out = new FileOutputStream("");OutputStream out2 = new FileOutputStream("")){

// ...操作流代码

} catch (Exception e) {

throw e;

}

5.内存流可以不用关闭(关与不关都可以,没影响)

ByteArrayOutputStream和ByteArrayInputStream其实是伪装成流的字节数组(把它们当成字节数据来看就好了),他们不会锁定任何文件句柄和端口,如果不再被使用,字节数组会被垃圾回收掉,所以不需要关闭。

6.使用装饰流时,只需要关闭最后面的装饰流即可

装饰流是指通过装饰模式实现的java流,又称为包装流,装饰流只是为原生流附加额外的功能(或效果),java中的缓冲流、桥接流也是属于装饰流。

InputStream is=new FileInputStream("C:\\Users\\tang\\Desktop\\记事本.txt");

InputStreamReader isr=new InputStreamReader(is);

BufferedReader br = new BufferedReader(isr);

String string = br.readLine();

System.out.println(string);

try {

br.close();//只需要关闭最后的br即可

} catch (Exception e) {

e.printStackTrace();

}

装饰流关闭时会调用原生流关闭,请看源码:

//BufferedReader.java

public void close() throws IOException {

synchronized (lock) {

if (in == null)

return;

try {

in.close();//这里的in就是原生流

} finally {

in = null;

cb = null;

}

}

}

有这样层层关闭的机制,我们就只需要关闭最外层的流就行了

7.关闭流时的顺序问题

out1 = new FileOutputStream("");

out2 = new FileOutputStream("");

//这里的out1和out2谁先关谁后关都一样,没有任何影响

如果两个流有依赖关系,那么你可以像上面说的,只关闭最外层的即可。

如果不嫌麻烦,非得一个个关闭,那么需要先关闭最里层,从里往外一层层进行关闭。

为什么不能从外层往里层逐步关闭?原因上面讲装饰流已经讲的很清楚了,关闭外层时,内层的流其实已经同时关闭了,你再去关内层的流,就会报错

至于网上说的先声明先关闭,就是这个道理,先声明的是内层,最先申明的是最内层,最后声明的是最外层。

其实jdk8版的顺序随便打乱关闭都不会报错,因为最里面的有判断,如果流已经关闭直接return)

8.深究为什么一定要关闭流的原因

一个流绑定了一个文件句柄(或网络端口),如果流不关闭,该文件(或端口)将始终处于被锁定(不能读取、写入、删除和重命名的)状态,占用大量系统资源却没有释放。

9.推荐使用NIO的Files工具类替换FileInputStream和FileOutputStream

public static List readAllLines(Path path, Charset cs)//以字符流方式读取所有行

public static Path write(Path path, Iterable lines, Charset cs, OpenOption… options)//以字符流方式写入指定行

public static byte[] readAllBytes(Path path)//以字节流方式读取所有字节

public static Path write(Path path, byte[] bytes, OpenOption… options)//以字节流方式写入指定字节 ———————————————

版权声明:本文为CSDN博主「思想永无止境」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/u012643122/article/details/38540721

相关推荐

铁血意志:德国传
be七365官网

铁血意志:德国传

📅 08-23 👁️ 8620
学生赚钱的 15 种好方法
求正规英国365网址

学生赚钱的 15 种好方法

📅 06-30 👁️ 9169
柔抛砖好不好?柔抛砖的优点和缺点,看完再决定买不买!
到底還要等多久
365bet中文官方网站

到底還要等多久

📅 07-27 👁️ 5891
解构阿里巴巴两大货源平台:1688/AliExpress
求正规英国365网址

解构阿里巴巴两大货源平台:1688/AliExpress

📅 08-03 👁️ 1347
社区发现Amazon美国亚马逊 秒杀时间显示 下午10:00...
365bet中文官方网站

社区发现Amazon美国亚马逊 秒杀时间显示 下午10:00...

📅 10-14 👁️ 4746