今日内容:
异常 分类 编译异常 运行异常
继承结构 处理异常try....catch....finally catch和finally中如果有异常怎严执行代码 catch编写方式
自定义异常 throw throws的作用
文件File 常用的方法 删除文件夹(空文件夹)
流 分类 读取 写出 缓冲流 序列化 /反序列化 ObjectInputStream /output Serializable
反序列化会创建新的对象
文件追加和覆盖
异常
定义:程序中一些程序处理不了的问题叫做异常
异常类 Exception
见过的异常 空指针异常NullPointerException 数组下标越界异常ArrayIndexoutOfBoundsException
异常的分类
检查型异常(编译异常)
在编译时就会抛出的异常(代码报错),需要在代码中编写处理方法 会和程序之外的代码资源访问 直接继承自Exception
运行时异常
在代码的运行阶段可能会出现的异常,可以没有明文处理
我们的运行异常可以通过代码避免异常的发生,继承自Exception的子类RunTimeException
出现异常的后果
当程序中出现异常,程序就会中断,代码不会继续运行
异常的处理
处理异常的唯一方法是try...catch...finally
throws仅仅只是抛出异常
throw和throws的区别:throw是抛出而throws仅仅是声明
//处理文件
File file = new File("D:\\easy.text");
//检查性异常(编译异常)
FileInputStream fis = null;
try {
//try块尝试捕捉异常 其中是可能会抛出异常的代码
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
//捕捉到异常后要处理的代码
e.printStackTrace();//打印异常日志
} finally {
//无论是否出现异常都会执行的代码块
//一般用在关闭资源
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Try 处理多种异常
try {
System.out.println(12 / 0);
Object strA = "";
System.out.println((Integer) strA);
fis = new FileInputStream(file);
} catch (ArithmeticException e) {
//出现数学运算异常
System.out.println("出现了ArithmeticException");
} catch (ClassCastException e) {
System.out.println("出现了ClassCastException");
} catch (FileNotFoundException e) {
System.out.println("出现了FileNotFoundException");
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
catch 合并处理方案 一个catch块捕捉多种异常 使用|声明多种异常
1.直接声明异常的父类
try {
System.out.println(12 / 0);
Object strA = "";
System.out.println((Integer) strA);
fis = new FileInputStream(file);
} catch (ArithmeticException | ClassCastException | FileNotFoundException e) {
}
//通过声明catch 块声明父类异常捕捉所有子类异常
try {
System.out.println(12 / 0);
Object strA = "";
System.out.println((Integer) strA);
fis = new FileInputStream(file);
} catch (Exception e) {
}
catch异常捕捉的顺序 子类异常优先处理,父类异常后置处理
2.依次处理
try {
List list = new ArrayList();
list.remove(8);//Indexout
int[] arr = new int[2];
arr[8] = 22;//ArrayIndex
String strA = "abc";
strA.charAt(8);//StringIndex
} catch (ArrayIndexOutOfBoundsException e) {//ArrayIndexOutOfBoundsException是Exception的子类所以要把ArrayIndexOutOfBoundsException放到前面优先处理
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(test());
}
如果catch块抛出了异常,如果没有finally就会中断异常
如果有finally就会执行finally 并且正常返回,此方法正常运行结束
public static int test() {
try {
System.out.println(12 / 0);
return 1;
} catch (Exception e) {
return 2;
} finally {
return 3;
}
}
try不能单独编写,必须有其他语句块 , 可以是catch 也可以是finally
//以catch 举例
public static void testA() {
try {
System.out.println(12 / 0);
} catch (Exception e) {
}
}
try块中没有检查性异常,不能在catch中随意捕捉
public static void testB() {
FileInputStream fis = null;
File fil = null;
try {
System.out.println(12 / 0);
//不能出现检查性异常,会导致报错
// fis = new FileInputStream(file);
} finally {
}
}
自定义异常
class BigStudent{
public void info() throws StudentNameIsNullException{}
}
class Student{
String name;
//throws声明方法中可能抛出的异常
//throws声明抛出多种异常
//方法重写:子类对从父类中继承来的方法进行重新定义
//约束:返回值类型 方法名 参数列表不能变
//访问权限只能更开放
//抛出的异常只能更精确(更小),不能变大
public void info() throws StudentNameIsNullException{
//name==null 是一种特殊情况不符合我们的业务需求
if(name==null){
throw new StudentNameIsNullException("student name is null");
}
System.out.println("我的名字是"+name);
}
//运行式异常可以不抛出
public void infoA(){
if(name==null){
// throw new NullPointerException("name is null");
throw new RuntimeException("具体说明");
}
}
}
检查性异常 是Exception的直接子类
class StudentNameIsNullException extends Exception{
public StudentNameIsNullException(){}
public StudentNameIsNullException(String msg){
super(msg);
}
}
异常之间的父子类关系
java中对文件
文件的创建和删除
public static void main(String[] args) {
//Java中声明一个文件,存入字符串当做文件地址
File f = new File("D:\\easy.txt");
//是否存在该文件
boolean bool = f.exists();exists的返回值是boolean
System.out.println(bool);
//创建文件
if (!bool) {
try {
bool = f.createNewFile();
if(bool){
System.out.println("成功创建文件");
}
} catch (IOException e) {
e.printStackTrace();
}
}else {
f.delete();//删除
System.out.println("成功删除文件");
}
删除文件夹时,文件夹必须是空的
文件和文件夹的判断
//获取是否是文件
bool = f.isFile();//是文件true不是文件false
System.out.println(bool);
//是否是文件夹
bool = f.isDirectory();
System.out.println(bool);
创建文件夹
创建文件夹
f.mkdir();//需要前面的路径都存在
f.mkdirs();//前面的路经可以不存在,若不存在会直接创建
IO
IO 输入流/输出流
流动的是数据 二进制
分类 :
根据流动的方向不同分为 输入流和输出流
根据流动的介质(单位)不同 分为字符流和字节流 字符流只能读取文本 .txt .xml .properties .html .yml
字节流什么文件都可以读取
根据功能(左右)不同 分为节点流和工具流 节点流和工具类 打印流 数据流 对象流
基本应用
public static void main(String[] args) {
//字节输入流
InputStream is;
//字节输出流
OutputStream os;
//字符输入流
Reader r;
//字符输出流
Writer w;
readObject();
}
public static void readFile() {
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);
// String.valueOf(arr);
System.out.print(str);
}
}catch (IOException e){
e.printStackTrace();
}finally {
if(fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
缓冲流(Buffered Streams)是对其他输入/输出流的一种包装,它增加了一个缓冲层,以提高数据的读写效率。
//文件字节输入流
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 = null;
System.out.println(line);
}catch (IOException e){
e.printStackTrace();
}finally{
if(fis != null){
try{
fis.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
序列化
public static void WriteObject(){
//将内存对象转换成序列(流),的过程叫做序列化
//这个对象必须是可序列化的
Staff staff = new Staff();
staff.name="张三";
staff.sex="男";
staff.salary=3500;
ObjectOutputStream oos=null;
FileOutputStream fos=null;
try{
fos = new FileOutputStream("D:\\easy.txt");
oos = new ObjectOutputStream(fos);
oos.writeObject(staff);
}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();
}
}
}
}
反序列化
//将对象序列读入程序转化为对象的方式:反序列化
//反序列化会创建新的对象
//总结:创建对象的方式
//1.new
//2.克隆
//3.反序列化
//4.反射