为什么把关闭资源放finally中, 放在try-catch之后不可以么?
1.第一种写法。
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream("d:/123.txt"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} finally {
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream("d:/123.txt"));
bos.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream("d:/123.txt"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
bos.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}