Java中的异常类和File类

异常类

在这里插入图片描述

那么我们先来看看运行异常的处理方法

public class MyTest {
    public static void main(String[] args) {
int i=1;
int n=0;
int[] arr={1,2,3};
arr=null;
try {
    int c=i/n;
    System.out.println(arr.length);
}catch (ArithmeticException e){
    System.out.println("除数为0了");
    e.printStackTrace();
}catch (NullPointerException e){
    System.out.println("空指针异常");
}catch (Exception e){
    System.out.println("其它异常");
}
        System.out.println("HelloWorld");
    }
}
输出结果:
java.lang.ArithmeticException: / by zero
除数为0了
	at com.xiyou.demo.MyTest.main(MyTest.java:10)
HelloWorld

关键字finally的使用

public class MyTest {
    public static void main(String[] args) {
        int i=1;
        int n=0;
        Scanner sc=null;
        try {
            System.out.println("i/n");
             sc = new Scanner(System.in);
            System.out.println("请输入一个整数");
        }catch (ArithmeticException e){
            e.printStackTrace();
        }finally {//一般我们会在finally里面做一些收尾工作,比如释放资源,它不管有没有错误都会执行
            System.out.println("执行了");
            if (sc!=null){
                sc.close();
            }
        }
    }
}

自定义的异常:

public class ScoreException extends RuntimeException {
    public ScoreException(String msg){
        super(msg);
    }
}
public class MyTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的成绩");
        int i = sc.nextInt();
        if (i>100||i<30) {
            throw new ScoreException("你输入的成绩有误");
        }else {
            System.out.println(i);
        }
    }
}

Java中常见的几种RuntimeException:
NullPointerException-空指针引用异常;
ClassCastException-类型强制转换异常;
IllegalArgumentException 传递非法参数异常;
ArithmeticException 算术运算异常;
ArrayStoreException 向数组中存放与声明类型不兼容对象异常:
indexOutofBoundsException: 下标越界异常
NegativeArraySizeException 创建一个大小为负的数组错误;
NumberFormatException 数字格式异常
SecurityException 安全异常
UnsupportedOperationException 不支持的操作异常;

File类

在这里插入图片描述

案例:输出我的D盘中MyTest文件夹中的后缀名是.png结尾的

public class MyTest {
    public static void main(String[] args) {
        File file = new File("D:\\MyTest");
        File[] files = file.listFiles();
        for (File file1 : files) {
            if (file1.isFile()&&file1.getName().endsWith(".png")){
                System.out.println(file1.getName());
            }
        }
    }
}
输出结果:
File类.png
Map集合.png
Set集合.png
Throwable类.png

文件过滤器的使用
public File[] listFiles(FilenameFilter filter)

案例:判断D盘目录下是否有后缀名为.png的文件,如果有,就输出该文件名称

public class MyTest {
    public static void main(String[] args) {
        File file = new File("D:\\MyTest");
        File[] files = file.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                File file1 = new File(dir, name);//dir: 表示的当前文件对应的父文件* name: 就是每一个文件对应的名称
                if (file1.isFile()&&file1.getName().endsWith(".png")){
                    return true;
                }else {
                    return false;
                }
            }
        });
        for (File file1 : files) {
            System.out.println(file1.getName());
        }
    }
}

案例:把上面获取到的文件名.png结尾的换成.jpg

public class MyTest {
    public static void main(String[] args) {
        File file = new File("D:\\MyTest");
        File[] files = file.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                File file1 = new File(dir, name);//dir: 表示的当前文件对应的父文件* name: 就是每一个文件对应的名称
                if (file1.isFile()&&file1.getName().endsWith(".png")){
                    return true;
                }else {
                    return false;
                }
            }
        });
        for (File file1 : files) {
            String s = file1.getName().replace(".png", ".jpg");
            File file2 = new File("D:\\MyTest", s);
            boolean b = file1.renameTo(file2);
            System.out.println(b);
        }
    }
}

案例3:利用递归删除嵌套的文件夹

public class MyTest {
    public static void main(String[] args) {
        File file = new File("D:\\MyTest");
       shanchu(file);
    }

    private static void shanchu(File file) {
        File[] files = file.listFiles();
        for (File file1 : files) {
            if (file1.isFile()){//判断是否是文件,是的话直接删
                file1.delete();
            }else {
                shanchu(file1);
            }
        } 
        file.delete();
    }
}

注意:删除之后回收站里面没有,所以要谨慎使用

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值