关闭流的操作,写在try-catch外面和写在finally里面有什么区别,都会执行的呀??!
public void test1() {
BufferedOutputStream bos = null;
try{
bos = new BufferedOutputStream(new FileOutputStream("d:/123.txt"));
}catch (Exception e){
System.out.println(e.getMessage());
} finally {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void test2(){
BufferedOutputStream bos = null;
try{
bos = new BufferedOutputStream(new FileOutputStream("d:/123.txt"));
}catch (Exception e){
System.out.println(e.getMessage());
}
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
像这种情况,是不是效果都是一样的,哪种写法好一点