---------------------- ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------
1.注意要对各个会发生IO异常的语句处理
2.对变量的定义,要考虑是局部还是全局都要使用3.考虑流对象是否能创建成功,若不成功,则不需要关闭流,所以关闭前要判断引用是否为空
import java.io.FileWriter;
import java.io.IOException;
public class IOExceptionHandle {
public static void main(String[] args) {
// TODO Auto-generated method stub
FileWriter fw = null; //若定义在try中,finally里中的fw.close()就不知道fw是什么了
try
{
fw = new FileWriter("demo.txt");
fw.write("abc");
}
catch(IOException e)
{
System.out.println("catch:"+e.toString());
}
finally
{
try
{
if(fw!=null) //若fw=null说明对象并未创建成功,则不需要关闭动作
fw.close();
}
catch(IOException e)
{
System.out.println("finallycatch:"+e.toString());
}
}
}
}
---------------------- ASP.Net+Android+IOS开发、 .Net培训、期待与您交流! ----------------------详细请查看: http://edu.csdn.net