import java.io.*;
class test{
public static void main(String[] args){
//局部变量没有默认值,成员变量有默认值,为了防止发生异常
//最好给局部变量赋初始值
FileWriter fw = null;//为了扩大作用范围
try{
fw = new FileWriter("temp.txt");
//fw = new FileWriter("K:\\temp.txt");//电脑中没有K盘,路径错误,所以导致FileWriter对象创建失败
//FileNotFoundException
fw.write("abcde");
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(fw!=null)
fw.close();//null.close()---NullPointerException空指针异常
}catch(IOException e){
throw new RuntimeException("写入流关闭失败");
}
}
}
}
Java——IO异常处理
最新推荐文章于 2024-06-03 17:29:15 发布