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

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<? extends CharSequence> 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

  • 14
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Java中的BorderLayout是一种常用的布局管理器,它将容器分为5个区域:北、南、东、西和中间。在使用BorderLayout时,我们可以通过设置组件的位置来确定它们在容器中的位置。 下面是一个简单的例子,演示如何使用BorderLayout: ``` import java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JFrame; public class BorderLayoutExample { public static void main(String[] args) { JFrame frame = new JFrame("BorderLayout Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton button1 = new JButton("North"); JButton button2 = new JButton("South"); JButton button3 = new JButton("East"); JButton button4 = new JButton("West"); JButton button5 = new JButton("Center"); frame.add(button1, BorderLayout.NORTH); frame.add(button2, BorderLayout.SOUTH); frame.add(button3, BorderLayout.EAST); frame.add(button4, BorderLayout.WEST); frame.add(button5, BorderLayout.CENTER); frame.pack(); frame.setVisible(true); } } ``` 在这个例子中,我们创建了一个JFrame,并在其中添加了5个JButton。使用BorderLayout将它们放置在窗口的不同位置。在上面的代码中,我们将button1放置在北面,button2放置在南面,button3放置在东面,button4放置在西面,button5放置在中心。 需要注意的是,当我们在使用BorderLayout时,如果某个位置没有被占用,那么它将自动被设置为空。另外,如果我们在同一个位置添加了多个组件,那么后添加的组件将覆盖先添加的组件。 总之,BorderLayout是一个非常常用的布局管理器,可以帮助我们轻松地将组件放置在容器中的不同位置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值