当前位置:   article > 正文

7-23学习笔记

7-23学习笔记

一、异常

        即程序中一些程序处理不了的特殊情况

        Exception 能被程序本身处理( try-catch )Error 是无法处理的(只能尽量避免)

1、异常类   Exception

见过的异常  NullPointerException  ArrayIndexoutOfBoundException等
  1. String str=null;
  2. System.out.println(str.length());//NullPointerException异常
  3. String name="张三";
  4. boolean bool=str.equals(name);//NullPointerException异常
  5. System.out.println(bool);
  6. int i=12;
  7. int a=0;
  8. System.out.println(i/a);//ArithmeticException异常

 2、异常的分类

        当程序中出现异常,就会中断程序,代码不会继续运行
(1)检查型异常(编译异常)

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

  1. File file=new File("D:\\easy.text");
  2. //检查性异常 编译异常
  3. FileInputStream fis=new FileInputStream(file);//FileNotFoundException

(2) 运行时异常

        在代码运行时可能会出现的异常,可以不用明文处理,而通过代码避免异常的发生 继承RunTimeException

  1. String str=null;
  2. //System.out.println(str.length());//NullPointerException异常
  3. if(str!=null){
  4. System.out.println(str.length());
  5. }else{
  6. System.out.println("str是null值");
  7. }
  8. String name="张三";
  9. //boolean bool=str.equals(name);//NullPointerException异常
  10. boolean bool=name.equals(str);
  11. System.out.println(bool);
  12. int i=12;
  13. int a=0;
  14. //System.out.println(i/a);//ArithmeticException异常
  15. if(a!=0){
  16. System.out.println(i/a);
  17. }else {
  18. System.out.println("a==0不能做除数");
  19. }

3、处理异常

        try...catch...finally
  1. File file=new File("D:\\easy.text");
  2. //检查型异常 编译异常
  3. //FileInputStream fis=new FileInputStream(file);//FileNotFoundException
  4. FileInputStream fis=null;
  5. try{
  6. //try块尝试捕捉异常 其中是可能会抛出异常的代码
  7. fis=new FileInputStream(file);
  8. //fis.close();
  9. }catch (FileNotFoundException e){
  10. //捕捉到异常后要处理的代码
  11. e.printStackTrace();//打印异常日志
  12. }finally {
  13. //无论是否出现异常都会执行的代码块
  14. //一般用在关闭资源
  15. if(fis!=null){
  16. try {
  17. fis.close();
  18. } catch (IOException e) {
  19. e.printStackTrace();
  20. }
  21. }
  22. }

4、处理多种异常

(1)try处理
  1. try {
  2. System.out.println(12/0);
  3. Object strA="";
  4. System.out.println((Integer)strA);
  5. fis=new FileInputStream(file);
  6. }catch (ArithmeticException e){
  7. //出现ArithmeticException要执行的代码
  8. System.out.println("出现ArithmeticException");
  9. }catch (ClassCastException e){
  10. System.out.println("出现ClassCastException");
  11. }catch (FileNotFoundException e){
  12. System.out.println("出现FileNotFoundException");
  13. }finally {
  14. if (fis!=null){
  15. try {
  16. fis.close();
  17. }catch (IOException e){
  18. e.printStackTrace();
  19. }
  20. }
  21. }
(2)合并处理方案 一个catch块捕捉多种异常
        使用 | 声明多种异常
  1. try {
  2. System.out.println(12/0);
  3. Object strA="";
  4. System.out.println((Integer)strA);
  5. fis=new FileInputStream(file);
  6. }catch (ArithmeticException | ClassCastException | FileNotFoundException e){
  7. }
(3)catch块声明父类异常,捕捉所有子类异常
  1. try {
  2. System.out.println(12/0);
  3. Object strA="";
  4. System.out.println((Integer)strA);
  5. fis=new FileInputStream(file);
  6. }catch (Exception e){
  7. }

5、catch 异常捕捉的顺序

        子类异常优先处理,父类异常后置处理
  1. try{
  2. List list=new ArrayList();
  3. list.remove(8);//Indexout
  4. int[] arr=new int[2];
  5. arr[8]=22;//ArrayIndex
  6. String strA="abc";
  7. strA.charAt(8);//StringIndex
  8. }catch (ArrayIndexOutOfBoundsException e){
  9. }catch (StringIndexOutOfBoundsException e){
  10. }catch (IndexOutOfBoundsException e){
  11. e.printStackTrace();
  12. }

 6、注意

        如果catch块抛出了异常,没有finally就会中断程序
        执行try 或者catch中的return语句之后,在返回之前,如果有finally,就会运行finally,如果finally中有return语句,那么程序就按finally中return,所以finally中的return是一定会被return的
  1. public class EasyException {
  2. public static void main(String[] args) {
  3. System.out.println(test());//3
  4. }
  5. public static int test(){
  6. try{
  7. System.out.println(12/0);
  8. return 1;
  9. }catch (Exception e){
  10. return 2;
  11. }finally {
  12. //System.out.println(12/0);
  13. return 3;
  14. }
  15. //只返回最后一次执行的返回值
  16. //若finally中没有return 且try中有异常,那么返回catch中的return
  17. //若finally中没有return 且try中没有异常,那么返回try中的return
  18. }
  19. }
      try不能单独编写 必须有其他语句块
      try块中没有检查性异常,不能在catch块中随意捕捉
 try...catch
  1. public static void testA(){
  2. try{
  3. System.out.println(12/0);
  4. }catch (Exception e){
  5. }
  6. }
try...finally
  1. public static void testA(){
  2. File file=null;
  3. FileInputStream fis=null;
  4. try{
  5. System.out.println(12/0);
  6. //不能出现检查型异常 该类异常必须用catch捕捉
  7. //fis=new FileInputStream(file);
  8. }finally {
  9. }
  10. }

7、自定义异常 

  1. //自定义一个检查型异常 是Exception的直接子类
  2. class StudentNameIsNullException extends Exception{
  3. public StudentNameIsNullException(){}
  4. public StudentNameIsNullException(String msg){
  5. super((msg));
  6. }
  7. }

8、throws、throw   

throws 声明方法可能要抛出哪种异常   声明抛出多个异常(用','连接)
throw 在方法体中抛出一个具体的异常对象  没有解决异常,只是抛给使用这段代码的人
运行时异常不用必须声明要抛出哪种异常  检查型异常则不行
  1. public void infoA(){
  2. if(name==null){
  3. throw new NullPointerException("name is null");
  4. //throw new RuntimeException(".....");
  5. //throw new Exception();
  6. }
  7. }

方法重写:子类对父类中继承过来的方法进行重新定义
约束:  返回值类型  方法名  参数列表不能变
访问权限只能更开放
抛出的异常只能更精确 范围应该更小,不能扩大(指检查型异常)
  1. class BigStudent extends Student{
  2. @Override
  3. public void info() throws StudentNameIsNullException {
  4. }
  5. }
  6. class Student{
  7. String name;
  8. public void info() throws StudentNameIsNullException,NullPointerException{
  9. //name==null是一个特殊情况 不符合业务需求
  10. if(name==null){
  11. throw new StudentNameIsNullException("Student name is null");
  12. }
  13. System.out.println("我的名字是"+name);
  14. }
  15. }

二、File  文件类

        java中对文件的处理  java.io包

 1、定义File类

File f=new File("D:\\easy.txt");

2、常用方法

(1)判断是否存在该文件  exists()
  1. boolean bool=f.exists();
  2. System.out.println(bool);
(2)创建文件   createNewFile()
  1. if(!bool){
  2. try{
  3. bool=f.createNewFile();
  4. if(bool){
  5. System.out.println("成功创建文件");
  6. }
  7. }catch (IOException e){
  8. e.printStackTrace();
  9. }
  10. }
(3)创建文件夹
  1. //创建时路径完整
  2. f.mkdir();
  3. //创建时路径不需要完整 会自己补全
  4. f.mkdirs();
(4)获取是否是文件
  1. bool=f.isFile();
  2. System.out.println(bool);
(5)是否是文件夹
  1. bool=f.isDirectory();
  2. System.out.println(bool);
(6)获取文件的大小
  1. f=new File("");
  2. long len=f.length();
  3. System.out.println(len);
(7)文件存在时删除文件  
  1. if(bool){{
  2. //删除文件夹时,文件夹必须是空的文件夹
  3. bool=f.delete();
  4. System.out.println("成功删除文件"+bool);
  5. }

        注:删除文件夹时,文件夹必须是空的文件夹

三、IO  输入输出流

        流动的是数据  数据的形式是二进制

1、分类

根据流动的方向不同     输入流和输出流
根据流动的介质(单位)不同    字符流和字节流
    字符流只能读取文本 .txt .xml .html .yml .properties
    字节流可以读取所有的文件类型
根据功能(作用)不同         节点流和工具流  打印流  数据流  对象流
  1. //字节输入流
  2. InputStream is;
  3. //字节输出流
  4. OutputStream os;
  5. //字符输入
  6. Reader r;
  7. //字符输出
  8. Writer w;

2、读取   写出 

(1)读取
  1. public static void readFile(){
  2. FileInputStream fis=null;
  3. try{
  4. fis=new FileInputStream("D:\\easy.txt");
  5. byte[] arr=new byte[12];
  6. //读取多少就转换多少
  7. int length=0;
  8. while((length=fis.read(arr))!=-1){
  9. //arr 中就是读到的数据
  10. String str=new String(arr,0,length);
  11. System.out.print(str);
  12. // String.valueOf(arr);
  13. }
  14. }catch (IOException e){
  15. e.printStackTrace();
  16. }finally {
  17. if(fis!=null){
  18. try{
  19. fis.close();
  20. }catch (IOException e){
  21. e.printStackTrace();
  22. }
  23. }
  24. }
  25. }
(2)写出
  1. public static void writeFile(){
  2. String str="埃弗拉看看发货了覅发记得你说过拉法基的阿凡达发生过阿斯弗而奋";
  3. byte[] arr=str.getBytes();
  4. FileOutputStream fos=null;
  5. try{
  6. //覆盖不加第二个参数,追加第二个参数置为true
  7. fos=new FileOutputStream("D:\\easy.txt",true);
  8. fos.write(arr);
  9. }catch (IOException e){
  10. e.printStackTrace();
  11. }finally {
  12. if(fos!=null){
  13. try{
  14. fos.close();
  15. }catch (IOException e){
  16. e.printStackTrace();
  17. }
  18. }
  19. }
  20. }

默认为覆盖式写出,若要改为追加式可在定义输出流时将第二个参数置为true

3、缓冲流

  1. public static void readFileBuffer(){
  2. //文件字节输入流
  3. FileInputStream fis=null;
  4. //工具流
  5. //转换流 字节流转换成字符流
  6. InputStreamReader isr=null;
  7. //缓冲流
  8. BufferedReader br=null;
  9. try{
  10. fis=new FileInputStream("D:\\easy.txt");
  11. isr=new InputStreamReader(fis);
  12. br=new BufferedReader(isr);
  13. String line=br.readLine();
  14. System.out.println(line);
  15. }catch (IOException e){
  16. e.printStackTrace();
  17. }finally {
  18. if(fis!=null){
  19. try{
  20. fis.close();
  21. }catch (IOException e){
  22. e.printStackTrace();
  23. }
  24. }
  25. }
  26. }

        是工具流的一种,当使用BufferedReader读取数据时,它会从底层流中读取一定数量的数据到内部缓冲区中进行暂时的保存,当读入到特定字符(如:\t \r)后一起发出

4、序列化与反序列化

(1)序列化

        将内存对象转换成序列(流),叫做序列化
        这个对象必须是可序列化的 即实现Serializable接口

  1. public static void wirteObject(){
  2. Staff staff=new Staff();
  3. staff.name="张三";
  4. staff.sex="男";
  5. staff.salary=3500;
  6. ObjectOutputStream oos=null;
  7. FileOutputStream fos=null;
  8. try{
  9. fos=new FileOutputStream("D:\\easy.txt");
  10. oos=new ObjectOutputStream(fos);
  11. oos.writeObject(staff);
  12. }catch (IOException e){
  13. e.printStackTrace();
  14. }finally {
  15. if(fos!=null){
  16. try{
  17. fos.close();
  18. }catch (IOException e){
  19. e.printStackTrace();
  20. }
  21. }
  22. if(oos!=null){
  23. try{
  24. oos.close();
  25. }catch (IOException e){
  26. e.printStackTrace();
  27. }
  28. }
  29. }
  30. }

对应的类:

  1. public class Staff implements Serializable {
  2. String name;
  3. String sex;
  4. int salary;
  5. @Override
  6. public String toString() {
  7. return "Staff{" +
  8. "name='" + name + '\'' +
  9. ", sex='" + sex + '\'' +
  10. ", salary=" + salary +
  11. '}';
  12. }
  13. }
(2)反序列化

         将对象序列读入程序, 转换成对象的方式:反序列化
         反序列化会创建新的对象

  1. public static void readObject(){
  2. FileInputStream fis=null;
  3. ObjectInputStream ois=null;
  4. try{
  5. fis=new FileInputStream("D:\\easy.txt");
  6. ois=new ObjectInputStream(fis);
  7. Object obj=ois.readObject();
  8. System.out.println(obj);
  9. }catch (Exception e){
  10. e.printStackTrace();
  11. }finally {
  12. if(fis!=null){
  13. try{
  14. fis.close();
  15. }catch (IOException e){
  16. e.printStackTrace();
  17. }
  18. }
  19. if(ois!=null){
  20. try{
  21. ois.close();
  22. }catch (IOException e){
  23. e.printStackTrace();
  24. }
  25. }
  26. }
  27. }
(3)序列化版本控制    transient关键词

        对于序列化对象,它的属性也必须是可序列化的,除非该属性是transient修饰的

serialVersionUID  是一个类的序列化版本号
如果该量没有定义,jdk会自动给与一个版本号,当该类发生变化时
该序列化版本号会发生变化,反序列化就会失败
自定义该版本号,只要该版本号不发生变化,即使类中属性/方法改变
该类的对象依旧可以反序列化
反序列化的对象是一个新的对象
  1. private static final long serialVersionUID=1L;
  2. private transient String sex;

 transient修饰属性 作用: 禁止属性的值被序列化

        当一个类的属性中有一个是不可序列化的类型或者不想被序列化时,可以使用transient禁止其序列化

 (4)输出流的flash方法
强制将缓冲区中的数据写出到目的地。
在某些情况下,缓冲区中的数据并不会自动写出到目的地,
而是需要显式地调用 flush 方法来实现。
在关闭输出流之前,通常会调用 flush 方法来确保所有待处理的数据都被写出到目的地。
大流套小流 大流执行了flush(),其嵌套的小流会自动执行flush()

  flush()虽然只对缓冲流有作用,但习惯上,所有的输出流在关闭前都会调用 flush()

  1. public static void writeStudent(Student stu){
  2. FileOutputStream fos=null;
  3. ObjectOutputStream oos=null;
  4. File file=new File("D:\\student.data");
  5. if(!file.exists()){
  6. try{
  7. file.createNewFile();
  8. }catch (IOException e){
  9. e.printStackTrace();
  10. }
  11. }
  12. try{
  13. fos=new FileOutputStream(file);
  14. oos=new ObjectOutputStream(fos);
  15. oos.writeObject(stu);
  16. oos.flush();
  17. //强制将缓冲区中的数据写出到目的地。
  18. // 在某些情况下,缓冲区中的数据并不会自动写出到目的地,
  19. // 而是需要显式地调用 flush 方法来实现。
  20. //在关闭输出流之前,通常会调用 flush 方法来确保所有待处理的数据都被写出到目的地。
  21. //大流套小流 大流执行了flush(),其嵌套的小流会自动执行flush()
  22. }catch (IOException e){
  23. e.printStackTrace();
  24. }finally {
  25. if(oos!=null){
  26. try{
  27. oos.close();
  28. }catch (IOException e){
  29. e.printStackTrace();
  30. }
  31. }
  32. if(fos!=null){
  33. try{
  34. fos.close();
  35. }catch (IOException e){
  36. e.printStackTrace();
  37. }
  38. }
  39. }
  40. }

 

补充总结:

创建对象的方法
 1 new
 2 克隆
 3 反序列化
 4 反射
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/899190
推荐阅读
相关标签
  

闽ICP备14008679号