JAVA IO流的异常处理方式(FileOutputStream)(内含jdk7之后)

本文详细介绍了JavaIO流在不同版本中的异常处理方法,包括try-catch-finally的使用、finally块中的close操作、jdk7引入的try-with-resources以及资源管理的最佳实践,同时讨论了调试问题。
摘要由CSDN通过智能技术生成

IO流的异常处理方式

jdk7之前(较繁琐不推荐)

try{}catch(){}代替throw(简单代码)

    public static void main(String[] args) throws IOException //这里用throw抛出异常
    {
        
            FileOutputStream fos = new FileOutputStream("D:\\A.txt");

            fos.write("abc".getBytes());
            fos.close();
    }
public static void main(String[] args) {
//用try{}catch(){}抛出异常
        try{
            FileOutputStream fos = new FileOutputStream("D:\\A.txt");

            fos.write("abc".getBytes());
            fos.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
  • 若是在 try 中加入另一个可以引发异常的代码,例如
try{
			//引发ArithmeticException: / by zero算术运算异常
            System.out.println(10 / 0);
            
            FileOutputStream fos = new FileOutputStream("D:\\A.txt");

            fos.write("abc".getBytes());
            fos.close();
        }catch (IOException e){
            e.printStackTrace();
        }

因为这个异常在close前面,一旦报异常,close将无法执行。所以字节输出流无法关闭。为了解决这一问题,我们可以将close放到finally中。

try{
            System.out.println(10 / 0);

            FileOutputStream fos = new FileOutputStream("D:\\A.txt");

            fos.write("abc".getBytes());
            
        }catch (IOException e){
            e.printStackTrace();
        }finally {
        //这时fos会标红 
        //因为fos权限不够
        //把fos提到上面
            fos.close();
        }
//虽然我们对fos赋值了,但发生异常时可能会无法执行
//所以我们先给他赋个null
//close还在标红 我们再给他抛个异常
FileOutputStream fos = null;
        try{
            System.out.println(10 / 0);

            fos = new FileOutputStream("D:\\A.txt");

            fos.write("abc".getBytes());

        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try{
                fos.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
  • 运行发现NullPointerException(fos确实是null)那么我们加个判断
try{
                if (fos != null) {
                    fos.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }

正确代码

public class FileOutputStreamDemo1 {

    public static void main(String[] args){

        FileOutputStream fos = null;
        try{

            fos = new FileOutputStream("D:\\A.txt",true);

            fos.write("您好您好".getBytes());

        } catch (IOException e){
            e.printStackTrace();
        } finally {

            if (fos != null) {
                try {
                    fos.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }

    }
}

jdk7之后(推荐)

public static void main(String[] args) {
			//将流对象直接放在try小括号里
        try(FileOutputStream fos = new FileOutputStream("D:\\A.txt");){
            fos.write("abc".getBytes());
        }catch (IOException e){
            e.printStackTrace();
        }
    }

格式:

try(需要调用close方法的对象){
	逻辑代码
}catch(异常类名 对象名){
	异常的处理方式
}

最后总结

这个怎么打断点,为啥不能调试,有同学知道吗?

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值