Java之如何关闭流

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Java之如何关闭流

思想永无止境   2022-11-23 我要评论

我们深知在操作Java流对象后要将流关闭,但往往事情不尽人意,大致有以下几种不能一定将流关闭的写法:

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;
}

注:Android SDK 20版本对应java7,低于20版本无法使用try-catch-resources自动关流。

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;
            }
        }
    }
//InputStreamReader.java
public void close() throws IOException {
        sd.close();//这里的sd就是原生流的解码器(StreamDecoder),解码器的close会调用原生流的close
    }

有这样层层关闭的机制,我们就只需要关闭最外层的流就行了(甚至博主认为,其实只关闭最里层的流也可以,因为装饰流并不持有任何文件句柄和端口,它和内存流一样是个“假流”,当然这只是我的个人推理,不敢保证只关闭最里层的流一定没有问题)。

7.关闭流时的顺序问题

两个不相干的流的关闭顺序没有任何影响,如:

out1 = new FileOutputStream("");
out2 = new FileOutputStream("");
//这里的out1和out2谁先关谁后关都一样,没有任何影响

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

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

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

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

分割线-----------------------------

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

可以看FileInputStream源码:

public void close() throws IOException {
        synchronized (closeLock) {
            if (closed) {
                return;
            }
            closed = true;
        }
        if (channel != null) {
           channel.close();
        }
 
        fd.closeAll(new Closeable() {
            public void close() throws IOException {
               close0();
           }
        });
    }

其他jdk版本,博主时间有限没有测试,各位还是遵循老办法(分割线前面的)关闭吧。

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

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

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

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

public static Path write(Path path, Iterable<? extends CharSequence> lines,
                             Charset cs, OpenOption... options)//以字符流方式写入指定行

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

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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们