Java学习DAY12

第十一章 异常

1.异常的概述和继承体系

在这里插入图片描述

2.JVM针对异常的默认处理方式

在这里插入图片描述

3.异常处理方案

在这里插入图片描述

public class cest {
    public static void main(String[] args){
        System.out.println("程序开始执行");
        method();
        System.out.println("程序结束执行");
        }
    public static void method(){
        try{
            int a=10;
            int b=0;
            System.out.println(a/b);
        }catch(ArithmeticException e){
            //System.out.println("除数不能为0");
            e.printStackTrace();
        }
    }
}

在这里插入图片描述

public class cest {
    public static void main(String[] args) throws ParseException {
        System.out.println("程序开始执行");
        try{
            method2();
        }catch(ParseException e){
            e.printStackTrace();
        }
        method1();       
        System.out.println("程序结束执行");
        }
    //运行时异常
    public static void method1() throws ArithmeticException {
            int a=10;
            int b=5;
            System.out.println(a/b);
            //System.out.println("除数不能为0");
        }
    //编译时异常
    public static void method2() throws ParseException{

                String s = "2021-06-03";
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date d = sdf.parse(s);
                System.out.println(d);

    }
}

4.两大异常

在这里插入图片描述

public class cest {
    public static void main(String[] args){
        System.out.println("程序开始执行");
        method1();
        method2();
        System.out.println("程序结束执行");
        }
    //运行时异常
    public static void method1(){
        try{
            int a=10;
            int b=0;
            System.out.println(a/b);
        }catch(ArithmeticException e){
            //System.out.println("除数不能为0");
            e.printStackTrace();
        }
    //编译时异常
    public static void method2() {
            try {
                String s = "2021-06-03";
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date d = sdf.parse(s);
                System.out.println(d);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

    }
}
5.File类

在这里插入图片描述

public class cest {
    public static void main(String[] args) throws ParseException {
        //创建文件对象的三种方法
        File f1 = new File("d:\\aa\\b.txt");
        File f2 = new File("d:\\aa","b.txt");//目录,文件名

        File f3 = new File("d:\\aa");
        File f4 = new File(f3,"b.txt");
    }
}

在这里插入图片描述

public class cest {
    public static void main(String[] args) throws IOException {
        File f1 = new File("d:\\aa\\b.txt");
        System.out.println(f1.createNewFile());//创建文件


        File f2= new File("d:\\bb");
        System.out.println(f2.mkdir());//创建目录

        File f3= new File("d:\\cc\\dd");
        System.out.println(f3.mkdirs());//创建多级目录

        File f4= new File("d:\\ee");
        File f5= new File("d:\\ee\\f.txt");
        System.out.println(f4.mkdir());
        System.out.println(f5.createNewFile());//创建指定目录下的指定文件
    }
}

在这里插入图片描述

public class cest {
    public static void main(String[] args) throws IOException {
        File f1 = new File("d:\\aa\\b.txt");
        System.out.println(f1.createNewFile());//创建文件
        System.out.println(f1.delete());//删除文件

        File f2= new File("d:\\bb");
        System.out.println(f2.mkdir());//创建目录
        System.out.println(f2.delete());//删除目录

    }
}

在这里插入图片描述

public class cest {
    public static void main(String[] args) throws IOException {
        File f1 = new File("aa\\b.txt");
        System.out.println(f1.isDirectory());//判断是否是目录
        System.out.println(f1.isFile());//判断是否是文件
        System.out.println(f1.exists());//是否存在



        System.out.println(f1.getAbsoluteFile());//获取绝对路径
        System.out.println(f1.getPath());//获取相对路径
        System.out.println(f1.getName());//获取名称

    }
}
6.IO流

在这里插入图片描述
在这里插入图片描述

public class cest {
    public static void main(String[] args) throws IOException {
        FileOutputStream f1 = new FileOutputStream("b.txt");

        //创建字节输出流对象做了以下三件事:
        //调用系统功能创建文件
        //创建字节输出流对象
        //让f1指向b.txt文件
        f1.write(65);
        f1.close();

    }
}

在这里插入图片描述

public class cest {
    public static void main(String[] args) throws IOException {
        FileOutputStream f1 = new FileOutputStream("b.txt");
        f1.write(65);//写一个字节

        byte[] by= {66,67,68,69};//写一个字节数组
        f1.write(by);


        byte[] bys="BCDE".getBytes();
        f1.write(bys);//写一个字符串

        f1.write("ABCDE".getBytes(),0,2);//写字符串的部分数据

        f1.close();
    }
}

在这里插入图片描述

public class cest {
    public static void main(String[] args) throws IOException {
        FileOutputStream f1 = new FileOutputStream("b.txt",true);//第二个参数为true,可以追加写入
        for(int i=0;i<10;i++){
            f1.write("ABCDE".getBytes());
            f1.write("\n".getBytes());//换行
        }
        f1.close();
    }
}

在这里插入图片描述

public class cest {
    public static void main(String[] args) throws IOException {
        FileInputStream f1 = new FileInputStream("b.txt");


        int by=f1.read();
        System.out.println(by);//读一个字

        int ay;
        while((ay=f1.read())!=-1){
            System.out.println((char)ay);//读取多个字
        }
        f1.close();
    }
}

在这里插入图片描述

public class cest {
    public static void main(String[] args) throws IOException {
        FileInputStream f1 = new FileInputStream("b.txt");
        byte[] by= new byte[1024];
        int len;
        while((len=f1.read())!=-1){
            System.out.println(new String(by,0,len));
        }
        f1.close();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值