1.JDK7之前的标准IO处理
/**
* JDK7之前的处理方式
*/
public static void tryCatch01(){
FileReader fr = null;
try {
fr = new FileReader("1.txt");
int ch = fr.read();
}catch (IOException ie){
ie.printStackTrace();
}finally {
//释放资源的代码
try {
if (fr != null) {
fr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.JDK7引入的IO处理
/**
* JDK7的处理方式
* try-with-resource
* 和资源一起try
*/
public static void tryCatch02(){
try(FileReader fr = new FileReader("1.txt")){
int ch = fr.read();
}catch (IOException ie){
ie.printStackTrace();
}
}
861



被折叠的 条评论
为什么被折叠?



