当前位置:   article > 正文

JAVA小白学习日记Day8

JAVA小白学习日记Day8

1.异常

异常:程序中一些程序处理不了的特殊情况。java中异常会以类的形式出现,当程序中出现异常,就会中断程序,代码不会继续运行。

异常分类:

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

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

处理异常:

1.throws:

在方法声明的位置上使用 throws 关键字抛出,谁调用我这个方法,我就抛给谁。抛给调用者来处理。这种处理异常的态度:上报。

throws的抛出范围更小,可以为空。声明方法中可能抛出的异常,声明抛出多种异常。

方法重写的约束:返回值类型,方法名,参数列表不能变,访问权限只能更开放。

 代码:

  1. package com.easy723;
  2. public class EasyExceptionA {
  3. }
  4. class BigStudent extends Student{
  5. @Override
  6. public void info () throws StudentNameIsNullException{
  7. //throws抛出范围更小,可以为空
  8. }
  9. }
  10. //自定义异常
  11. class Student {
  12. String name;
  13. //throws 声明方法中可能抛出的异常
  14. //throws 声明抛出多种异常
  15. //方法重写:子类对父类中继承过来的方法进行重新定义
  16. //约束:返回值类型,方法名,参数列表不能变,访问权限只能更开放。重写的方法抛出的异常,只能更精确,范围更小,不能扩大。
  17. public void info() throws StudentNameIsNullException,NullPointerException{
  18. //name==null是一种特殊情况,不符合业务需求
  19. if (name==null){
  20. throw new StudentNameIsNullException("student name is null");
  21. }
  22. System.out.println("我的名字是:"+name);
  23. }
  24. //运行时异常可以不声明抛出
  25. public void infoA(){
  26. if (name==null){
  27. throw new NullPointerException("name is null");
  28. }
  29. }
  30. }
  31. //检查型异常 是Exception的直接子类
  32. class StudentNameIsNullException extends Exception{
  33. public StudentNameIsNullException(){
  34. }
  35. public StudentNameIsNullException(String msg){
  36. super(msg);
  37. }
  38. }

2.try…catch...finally:

这个异常不会上报,自己把这个事儿处理了。异常抛到此处为止,不再上抛了。

注意:

只要异常没有捕捉,采用上报的方式,此方法的后续代码不会执行。当try处理多种异常时,遇到第一个异常就中断,跳转到catch。

try语句块中的某一行出现异常,该行后面的代码不会执行。try不能单独编写,必须有其他语句块。

try…catch捕捉异常之后,后续代码可以执行。

finally块无论是否出现异常都会执行的代码块,一般用在关闭资源。如果只有finally,try中不能有检查型异常。try块中没有检查型异常,不能再catch块中随意捕捉。

catch块是合并处理方案,一个catch块捕捉多种异常。可以通过声明父类异常来捕捉所有子类异常,catch异常捕捉的顺序:子类异常优先处理(前放),父类异常后处理。如果catch块抛出异常,没有finally就会中断程序,如果有,运行finally并且正常返回,此方法结束。

代码:

  1. package com.easy723;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. public class EasyException {
  9. //异常 程序中一些程序处理不了的特殊情况
  10. //异常类 Exception
  11. //见过的异常:Null Pointer Exception,ArrayIndexoutOf...
  12. public static void main(String[] args) {
  13. String str=null;
  14. // String name="张三";
  15. // boolean b=str.equals(name);
  16. // b=name.equals(str);//把确定不为空的放前面避免异常
  17. //System.out.println(str.length());
  18. int i=12;
  19. int a=0;
  20. if(a!=0) {
  21. System.out.println(i / a);//没有if判断直接运行会报错:ArithmeticException
  22. }else {
  23. System.out.println("a不能做除数");
  24. }
  25. System.out.println("++++++++++++");
  26. //当程序中出现异常,就会中断程序,代码不会继续运行
  27. //异常分类:
  28. //1.检查型异常(编译异常):在编译时就会抛出的异常(在代码上报错),需要在代码中编写处理方式
  29. //和程序之外的资源访问 直接继承Exception
  30. //2.运行时异常:在代码运行阶段可能会出现的异常,可以没有明文处理,可以通过代码避免异常的发生
  31. //继承RunTimeException
  32. //处理异常
  33. //try...catch...finally
  34. //声明抛出 throws
  35. //声明异常
  36. //处理文件
  37. File file=new File("D:\\easy.test");
  38. //检查型异常(编译异常)
  39. FileInputStream fis=null;//因为finally要调用fis,所以在外部声明
  40. try {
  41. //尝试捕捉异常 其中可能会抛出异常的代码
  42. fis=new FileInputStream(file);
  43. }catch (FileNotFoundException e){
  44. //捕捉到异常后要处理的代码
  45. e.printStackTrace();//打印异常日志
  46. }finally {
  47. //无论是否出现异常都会执行的代码块 一般用在关闭资源
  48. if (fis!=null){
  49. try {
  50. fis.close();
  51. } catch (IOException e) {
  52. e.printStackTrace();
  53. }
  54. }
  55. }
  56. //try 处理多种异常
  57. try {
  58. System.out.println(12/0);//第一个异常就中断,跳到catch,后两个异常不会运行
  59. Object strA="";
  60. System.out.println((Integer)strA);
  61. fis=new FileInputStream(file);
  62. }catch (ArithmeticException e){
  63. //出现ArithmeticException要执行的代码
  64. System.out.println("出现ArithmeticException");
  65. }catch (ClassCastException e){
  66. System.out.println("出现ClassCastException");
  67. }catch (FileNotFoundException e){
  68. System.out.println("出现FileNotFoundException");
  69. }finally {
  70. if (fis!=null){
  71. try {
  72. fis.close();
  73. } catch (IOException e) {
  74. e.printStackTrace();
  75. }
  76. }
  77. }
  78. //catch 合并处理方案 一个catch块捕捉多种异常
  79. //1.
  80. try {
  81. System.out.println(12/0);
  82. Object strA="";
  83. System.out.println((Integer)strA);
  84. fis=new FileInputStream(file);
  85. }catch (ArithmeticException|ClassCastException|FileNotFoundException e){
  86. }
  87. //2.通过声明父类异常,捕捉所有子类异常
  88. try {
  89. System.out.println(12/0);
  90. Object strA="";
  91. System.out.println((Integer)strA);
  92. fis=new FileInputStream(file);
  93. }catch (Exception e){
  94. }
  95. //catch异常捕捉的顺序 子类异常优先处理(前放),父类异常后置处理
  96. try {
  97. List list=new ArrayList<>();
  98. list.remove(8);//Indexout
  99. int[] arr=new int[2];
  100. arr[8]=22;//ArrayIndex
  101. String strA="abc";
  102. strA.charAt(8);//Stringindex
  103. }catch (ArithmeticException e){
  104. e.printStackTrace();
  105. }catch (StringIndexOutOfBoundsException e){
  106. e.printStackTrace();
  107. }catch (IndexOutOfBoundsException e){
  108. e.printStackTrace();
  109. }
  110. System.out.println(test());//3
  111. }
  112. //如果catch块抛出异常,没有finally就会中断程序
  113. //如果有finally,会运行finally并且正常返回,此方法正常运行结束
  114. public static int test(){
  115. try {
  116. System.out.println(12/0);
  117. return 1;
  118. }catch (Exception e){
  119. return 2;
  120. }finally {
  121. return 3;
  122. }
  123. }
  124. //try不能单独编写,必须有其他语句块
  125. public static void testA(){
  126. try {
  127. System.out.println(12/0);
  128. }catch (Exception e){
  129. }
  130. }
  131. public static void testB(){
  132. FileInputStream fis=null;
  133. File file=null;
  134. try {
  135. System.out.println(12/0);
  136. //如果只有finally,try中不能有检查型异常
  137. //比如fis=new FileInputStream(file);
  138. }finally {
  139. }
  140. }
  141. //try块中没有检查型异常,不能在catch块中随意捕捉
  142. public static void testC(){
  143. try {
  144. int a=12+8;
  145. }catch (NullPointerException e){
  146. //可以
  147. }
  148. // catch (FileNotFoundException e){
  149. // 不行
  150. // }
  151. }
  152. }

2.文件(IO流)

java中对文件的处理通过java.io包。

在声明一个文件时,传入字符串当作文件地址。

注意:创建文件时,mkdir创建必须前面的路径都存在,mkdirs不用。删除文件夹时,文件夹必须是空的。

一些常用操作代码:

  1. package com.easy723;
  2. import java.io.File;
  3. import java.io.IOException;
  4. public class EasyFile {
  5. //java中对文件的处理
  6. //java.io包
  7. public static void main(String[] args) {
  8. //在java中声明一个文件 传入字符串当作文件地址
  9. File f=new File("D:\\easy.txt");
  10. //是否存在该文件
  11. boolean bool=f.exists();
  12. System.out.println(bool);
  13. //创建文件
  14. if (!bool){
  15. try {
  16. bool=f.createNewFile();
  17. if (bool){
  18. System.out.println("成功创建文件");
  19. }
  20. }catch (IOException e){
  21. e.printStackTrace();
  22. }
  23. }else {
  24. //删除文件夹时,文件夹必须是空的文件夹
  25. bool=f.delete();
  26. System.out.println("成功删除文件"+bool);
  27. }
  28. //获取是否是文件
  29. bool=f.isFile();
  30. System.out.println(bool);
  31. //是否是文件夹
  32. bool=f.isDirectory();
  33. System.out.println(bool);
  34. //创建文件
  35. //mkdir创建必须前面的路径都存在,mkdirs不用
  36. //f.mkdir();
  37. }
  38. }

IO流(输入/输出流)

流:流动的数据,是二进制。

分类:根据流动的方向不同:输入流(向java程序流)和输出流。

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

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

总结:创建对象的方式

1.new     2.克隆      3.反序列化       4.反射

序列化和反序列化:

序列化:将内存对象转换成序列(流)。这个对象必须是可序化的。

优点:

实现了数据的持久化,通过序列化可以把数据持久地保存在硬盘上(磁盘文件)。

利用序列化实现远程通信,在网络上传输字节序列。

反序列化:将对象序列读入程序,转换成对象的方式。反序列化会创建新对象,与之前的不是一个数据。

工具流:

1.转换流

字符流比字节流在操作上更加方便,Java提供了转换流来实现字节流向字符流的转换。

2.缓冲流

缓冲流:增强基本流的功能,在基本流的基础上演变而来。
按照数据类型进行分类分为:
字节缓冲流:BufferedInputStrem  BufferedOutputStream
字符缓冲流:BufferedReader  BufferedWriter

缓冲流的基本原理:是在创建流对象时会创建一个内置的默认大小的缓冲区数组,通过缓冲区读写,减少系统的IO次数,提高读写的效率。

代码:

  1. package com.easy723;
  2. import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
  3. import java.io.*;
  4. public class EasyFileIO {
  5. //IO 输入流/输出流
  6. //流动的是数据 二进制
  7. //分类:根据流动的方向不同 输入流(向java程序流) 输出流
  8. //根据流动的介质(单位)不一样 字符流和字节流
  9. //字符流只能读取文本txt .xml .html .properties
  10. //字节流可以读取所有的文件类型
  11. //根据功能(作用)不一样 节点流和工具流 打印流 数据流 对象流
  12. public static void main(String[] args) {
  13. //字节输入流
  14. InputStream is;
  15. //字节输出流
  16. OutputStream os;
  17. //字符输入流
  18. Reader r;
  19. //字符输出流
  20. Writer w;
  21. //
  22. //readFile();
  23. //writeFile();
  24. //readFileBuffer();
  25. //writeObject();
  26. readObject();
  27. //总结:创建对象的方式
  28. //1.new
  29. //2.克隆
  30. //3.反序列化
  31. //4.反射
  32. }
  33. public static void readObject(){
  34. //将对象序列读入程序,转换成对象的方式:反序列化
  35. //反序列化会创建新的对象
  36. FileInputStream fis=null;
  37. ObjectInputStream ois=null;
  38. try {
  39. fis=new FileInputStream("D:\\easy.txt");
  40. ois=new ObjectInputStream(fis);
  41. Object obj=ois.readObject();
  42. System.out.println(obj);
  43. }catch (Exception e){
  44. e.printStackTrace();
  45. }finally {
  46. if (ois!=null){
  47. try {
  48. ois.close();
  49. }catch (IOException e){
  50. e.printStackTrace();
  51. }
  52. }
  53. if (fis!=null){
  54. try {
  55. fis.close();
  56. }catch (IOException e){
  57. e.printStackTrace();
  58. }
  59. }
  60. }
  61. }
  62. public static void writeObject(){
  63. //将内存对象转换成序列(流),叫做序列化
  64. //这个对象必须是可序列化的
  65. Staff staff=new Staff();
  66. staff.name="张三";
  67. staff.sex="男";
  68. staff.salary=3500;
  69. ObjectOutputStream oos=null;
  70. FileOutputStream fos=null;
  71. try {
  72. fos=new FileOutputStream("D:\\easy.txt");
  73. oos=new ObjectOutputStream(fos);
  74. oos.writeObject(staff);
  75. }catch (IOException e){
  76. e.printStackTrace();
  77. }finally {
  78. if (oos!=null){
  79. try {
  80. oos.close();
  81. }catch (IOException e){
  82. e.printStackTrace();
  83. }
  84. }
  85. if (fos!=null){
  86. try {
  87. fos.close();
  88. }catch (IOException e){
  89. e.printStackTrace();
  90. }
  91. }
  92. }
  93. }
  94. public static void writeFile(){
  95. String str="哈哈哈哈哈";
  96. byte[] arr=str.getBytes();
  97. FileOutputStream fos=null;
  98. try {
  99. fos=new FileOutputStream("D:\\easy.txt",true);
  100. fos.write(arr);
  101. }catch (IOException e){
  102. e.printStackTrace();
  103. }finally {
  104. if (fos!=null){
  105. try {
  106. fos.close();
  107. }catch (IOException e){
  108. e.printStackTrace();
  109. }
  110. }
  111. }
  112. }
  113. public static void readFileBuffer(){
  114. FileInputStream fis=null;//文件字节输入流
  115. // 工具流
  116. //转换流:字节流转换成字符流
  117. InputStreamReader isr=null;
  118. //缓冲流:
  119. BufferedReader br=null;
  120. try{
  121. fis=new FileInputStream("D:\\easy.txt");
  122. isr=new InputStreamReader(fis);
  123. br=new BufferedReader(isr);
  124. String line=br.readLine();
  125. System.out.println(line);
  126. }catch (IOException e){
  127. e.printStackTrace();
  128. }finally {
  129. if (fis!=null){
  130. try {
  131. fis.close();
  132. }catch (IOException e){
  133. e.printStackTrace();
  134. }
  135. }
  136. }
  137. }
  138. public static void readFile(){
  139. FileInputStream fis=null;
  140. try{
  141. fis=new FileInputStream("D:\\easy.txt");
  142. byte[] arr=new byte[6];
  143. //读取多少转换多少
  144. int length=0;
  145. while((length=fis.read(arr))!=-1){
  146. //arr中就是读取的数据
  147. String str=new String(arr,0,length);
  148. //或者String.valueOf(arr);
  149. System.out.print(str);
  150. }
  151. }catch (IOException e){
  152. e.printStackTrace();
  153. }finally {
  154. if (fis!=null){
  155. try{
  156. fis.close();
  157. }catch (IOException e){
  158. e.printStackTrace();
  159. }
  160. }
  161. }
  162. }
  163. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/907311
推荐阅读
相关标签
  

闽ICP备14008679号