异常
异常是指程序中一些程序处理不了的特殊情况
异常类 Exception
见过的异常:NullPointerException ArrayIndexoutofBoundException
当程序出现异常,代码不会继续执行
处理异常常用try...catch...finally
try不能单独使用,catch可以没有,finally也可以没有,但不能同时没有。如果只有try...finally,此时try语句中不能有检查性异常语句。
try尝试捕捉异常 其中是可能会抛出异常的代码;
捕捉到异常后要处理的代码;
无论是否出现异常都会执行的代码块,一般用在关闭资源。
try...catch...finally的使用实例
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...catch...finally语句可以有多个catch语句。当有多个catch语句可以使用合并处理方案,使用一个代码块捕捉多种异常。合并处理的方案有:
将异常用|连接
使用父类异常,捕捉所有子类异常。catch 异常捕捉的顺序,子类异常优先处理,父类异常后置处理。
try{
System.out.println(12/0);
Object st = "";
System.out.println((Integer)st);
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{
try {
fis.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
//合并异常(将异常用|连接)
catch(ArithmeticException|ClassCastException|FileNotFoundException e){
System.out.println("出现异常");
}
//使用父类异常,捕捉所有子类异常
catch(Exception e){
System.out.println("出现异常");
}
如果catch块抛出了异常,没有finally就会中断程序
如果有finally,会运行finally并且正常返回,此方法正常运行。
finally没有捕捉异常的功能,虽然finally里的代码一定会被执行,但由于发生了异常,所以程序异常结束。
public static int test(){
try{
System.out.println(12/0);
return 1;
}catch (Exception e){
return 2;
}finally{
//finally没有捕捉异常的功能,虽然finally里的代码一定会被执行,但由于发生了异常,所以程序异常结束
System.out.println(12/0);
return 3;
}
}
异常的分类:
检查性异常(编译异常):在编译时就抛出的异常(代码上会报错)
需要在代码中编写处理方式 (和程序之外的内容)
运行时异常:在代码的运行阶段可能会出现的异常,可以不用明文处理
可以通过代码避免异常的发生 继承RunTimeException
方法重写的约束:
约束: 返回值类型 方法名 参数列表不能变
访问权限只能更开放 抛出的异常只能更精确(范围更小),不能扩大
throws:
修饰符 返回类型 方法名(参数列表) throws 异常类型1, 异常类型2, ... {
// 方法体}
public void info() throws StudentNAmeIsNullException{
throws可以跟多个异常类名,用逗号隔开
throws: 表示抛出异常,由该方法的调用者来处理
throws: throws表示有出现异常的可能性,并不一定出现这些异常 throw: throw则是抛出了异常,执行throw一定出现了某种异常
throw:
throw new StudentNAmeIsNullException();throw后跟异常方法
throw: 只能抛出一个异常对象名
throw: 表示抛出异常,由该方法体内的语句来处理
throw: throw则是抛出了异常,执行throw一定出现了某种异常
//自定义异常
class Student {
String name;
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");
}
}
}
//检查性异常 是Exception的直接子类
class StudentNAmeIsNullException extends Exception{
public StudentNAmeIsNullException(){}
public StudentNAmeIsNullException(String msg){
super(msg);
}
}
File文件
java中对文件引入java.io包
常见的引入包有:java.long java.util
java中文件类的使用,声明一个文件,传入字符串当做文件地址
File f=new file(“在此处输入存放创建文件的地址”)
一些方法的调用
查看是否存在该文件
boolean bool=f.exists();
System.out.println(bool);
if(!bool){
try{
bool=f.createNewFile();
if (bool){
System.out.println("成功创建文件");
}
}catch (IOException e){
e.printStackTrace();
}
}
删除文件夹是,文件夹必须是空的
//查看是否是文件
boolean b=f.isFile();
System.out.println(b);
//是否是文件夹
boolean a = f.isDirectory();
System.out.println(a);
IO流
IO 输入流 输出流
在流中流动的是数据,是二进制
分类:根据流动单位的不同,分为字节流和字符流
字节流可以读取所有的文件类型
根据功能和作用的不同,分为节点流和工具流
字节输入流 InputStream is;
字节输出流 OutputStream os;
字符输入流 Reader r;
字符输出流 Writer w;
缓冲流是一个包装流,目的是缓存作用,加快读取和写入数据的速度。
字节缓冲流:
写入数据到流中,字节缓冲输出流 BufferedOutputStream
读取流中的数据,字节缓冲输入流 BufferedInputStream
字符缓冲流:BufferedReader、BufferedWriter
缓冲流属于包装流,只能对已有的流进行封装,不能直接关联文件进行操作
FileInputStream(文件输入流)从文件中读取字节数据示例。
public static void readFile(){
FileInputStream fis = null;
try{
fis = new FileInputStream("C:Users\\21700\\Desktop\\ph\\easy.txt");
byte[] arr = new byte[8];
//读取多少就转换多少
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();
}
}
}
}
用了缓冲流(BufferedReader)和转换流(InputStreamReader)实现文件中字符的完整(整块读取)。BufferedReader提供了缓冲区功能,可以一次读取多个字符,减少了频繁的系统调用,从而提高了读取文件的效率。它还提供了方便的方法(如 readLine()来逐行读取文本数据)。
public static void readFileBuffer(){
//转换流
InputStreamReader isr = null;
//缓冲流
FileInputStream fis = null;
BufferedReader br = null;
try{
fis = new FileInputStream("C:Users\\21700\\Desktop\\ph\\easy.txt");
isr = new InputStreamReader(fis);
br = new BufferedReader(isr);
String s2 = br.readLine();
System.out.println(s2);
}catch(IOException e){
e.printStackTrace();
}finally{
if(fis!=null){
try{
fis.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
向文件中写入字符串,使用创建一个文件输出流用于向 easy.txt 文件追加写入数据。第二个参数 true 表示追加模式,将新写入的内容追加到文件的末尾(若省略第二位参数则自动默认覆盖原来的内容)。
public static void writeFile(){
String s3="今天也是很帅";
byte[] ar = s3.getBytes();
FileOutputStream fos = null;
try{
fos = new FileOutputStream("C:Users\\21700\\Desktop\\ph\\easy.txt",true);
fos.write(ar);
}catch(IOException e){
e.printStackTrace();
}finally{
if(ar!=null){
try{
fos.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
序列化与反序列化:一个对象要实现可序列化要满足创建对象的类实现Serializable接口。
public class Staff implements Serializable{}
将内存对象转换成序列(流)的过程叫做序列化,这个对象必须是可序列化的。
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("C:Users\\21700\\Desktop\\ph\\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();
}
}
}
}
创建对象的方式:new ,克隆 ,反序列化 ,反射
将对象序列读入程序,转换成对象的方式:反序列化。反序列化会创建新的对象。
public static void readObject(){
//将对象序列读入程序,转换成对象的方式:反序列化
//反序列会创建新的对象
FileInputStream fis = null;
ObjectInputStream ois = null;
try{
fis = new FileInputStream("C:Users\\21700\\Desktop\\ph\\easy.txt");
ois = new ObjectInputStream(fis);
Object obj = ois.readObject();
System.out.println(obj);
}catch(Exception e){
e.printStackTrace();
}finally {
if(fis!=null){
try{
fis.close();
}catch(IOException e){
e.printStackTrace();
}
}
if(ois!=null){
try{
ois.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}