程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

Java学习DAY12

发布于2021-06-12 15:27     阅读(206)     评论(0)     点赞(16)     收藏(4)


第十一章 异常

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();
    }
}

原文链接:https://blog.csdn.net/gtt683559/article/details/117530992



所属网站分类: 技术文章 > 博客

作者:忽明忽灭

链接:http://www.javaheidong.com/blog/article/222532/c89e3c9bd61ea67b683b/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

16 0
收藏该文
已收藏

评论内容:(最多支持255个字符)