java学习笔记8

一、异常处理

1.异常:程序中一些程序处理不了的特殊情况

2.异常类 Exception

3.异常的分类:

(1).检查型异常(编译异常):在编译时就会抛出的异常(代码上会报错),需要在代码中编写处理方式 (和程序之外的资源访问) 直接继承Exception

(2).运行时异常: 在代码运行阶段可能会出现的异常,可以不用明文处理可以通过代码避免异常的发生 继承RunTimeException

4.处理异常

try...catch...finally
try块尝试捕捉异常 其中是可能会抛出异常的代码
catch(要捕获的异常对象){捕捉到异常后要处理的代码}
finally块写无论是否出现异常都会执行的代码块

5.try 可同时捕获多种异常

   catch 合并处理方案 一个catch块捕捉多种异常,使用 | 分隔声明多种异常

   catch块声明父类异常,捕捉所有子类异常

6. catch异常捕捉的顺序 子类异常优先处理,父类异常后置处理

7. 如果catch块抛出了异常, 没有finally就会中断程序,如果有finally,会运行finally会运行 并且正常返回,此方法正常运行结束

8. try不能单独编写必须有其他语句块

9. try块中没有检查型异常,不能在catch块中随意捕捉

二、抛出异常

1. throws 声明方法中可能抛出的异常,throws 声明方法中可抛出的异常

2.重写的方法抛出的异常只能更精确(范围更小),不能扩大

3. 自定义异常

定义异常类

//检查性异常   是Exception的直接子类
class StudentNameIsNullException extends Exception{
    public StudentNameIsNullException(){}
    public StudentNameIsNullException(String msg){
        super(msg);
    }

使用throw抛出

throw new StudentNameIsNullException("student name is null");
三、文件

1. 创建文件对象

 File f=new File("D:\\easy.txt");//创建一个文件对象f

2. 判断文件是否存在

f.exists();//该方法返回一个布尔值

3. 创建文件

f.createNewFile();

4.删除文件(删除文件夹时文件夹必须是空的)

f.delete();

5.判断是文件还是文件夹

f.isFile();
isDirectory();
//这两个方法会返回一个布尔值

6. 创建文件夹

f.mkdir();

7. 获取文件大小

f.length();
四、IO流

1. 输入流/输出流

    流动的是二进制数据

2. 分类:

(1)根据流动的方向不同   输入流和输出流

(2)根据流动的介质(单位)不同   字符流和字节流
(字符流只能读取文本 txt .xml .properties .html .yml,字节流可以读取所有的文件类型)

(3)根据功能(作用)不同   节点流和工具流   打印流   数据流   对象流

3.InputStream字节输入流,OutputStream字节输出流,Reader字符输入流,Writer字符输出流

4.序列化与反序列化:

(1)将内存对象转换成序列(流),叫做序列化,这个对象必须是可序列化的

        ObjectOutputStream oos=null;
        FileOutputStream fos=null;
        try{
            fos=new FileOutputStream("D:\\easy.txt");
            oos=new ObjectOutputStream(fos);
            oos.writeObject(obj);
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            if(oos!=null){
                try{
                    oos.close();
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
            if(fos!=null){
                try{
                    fos.close();
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
        }

(2)将对象序列读入程序,转换成对象的方式:反序列化

        FileInputStream fis=null;
        ObjectInputStream ois=null;
        try{
            fis=new FileInputStream("D:\\easy.txt");
            ois=new ObjectInputStream(fis);
            Object obj=ois.readObject();
            System.out.println(obj);
            return obj;
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }finally{
            if(ois!=null){
                try{
                    ois.close();
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
            if(fis!=null){
                try{
                    fis.close();
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
        }

5. 将流写出到文件

        String str="test";
        byte[] arr=str.getBytes();
        FileOutputStream fos=null;
        try{
            fos=new FileOutputStream("D:\\easy.txt",true);
            fos.write(arr);
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            if(fos!=null){
                try{
                    fos.close();
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
        }

6.读取文件

        FileInputStream fis=null;
        try{
            fis=new FileInputStream("D:\\easy.txt");
            byte[] arr=new byte[16];
            //读取多少就转换多少
            int length=0;
            while((length=fis.read(arr))!=-1){
                //arr 中就是读取到的数据
                String str=new String(arr,0,length);
                System.out.print(str);
                //String.valueOf(arr);
            }

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

7.缓冲流

        //文件字节输入流
        FileInputStream fis=null;
        //工具流
        //转换流
        InputStreamReader isr=null;
        //缓冲流
        BufferedReader br=null;
        try{
            fis=new FileInputStream("d:\\easy.txt");
            isr=new InputStreamReader(fis);
            br=new BufferedReader(isr);
            String line=br.readLine();
            System.out.println(line);
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            if(fis!=null){
                try{
                    fis.close();
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
        }

8. 流使用完要关闭,以节省资源,一般关闭流的操作在finally块中执行,使用fos.close();

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值