Day19(异常/file类)
异常
异常的概述和分类
A:异常的概述
*异常就是Java程序在运行过程中出现的错误;
B:异常的分类
*通过API查看Throwable
*Error
*服务器宕机,数据库崩溃等;
*Exception
C:异常的继承体系
*Throwable
*Error;
*Exception
*RuntimeException
JVM是如何处理异常的
class Demo {
public int div(int a,int b) {
return a / b;
被除数是10,除数是0当除数是0的时候违背了算数运算法则,抛出异常
}
}
异常处理
public static void main(String[] args) {
Demo2 d = new Demo2();
try {
int x = d.div(10, 0);
System.out.println(x);
} catch (ArithmeticException a) {
System.out.println("出错了,除数为零了");
}
}
public static void demo1() {
int a = 10;
int b = 0;
int[] arr = {11,22,33,44,55};
try {
System.out.println(a / b);
System.out.println(arr[10]);
arr = null;
System.out.println(arr[0]);
} catch (ArithmeticException e) {
System.out.println("除数不能为零");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("索引越界了");
} catch (Exception e) {
System.out.println("出错了");
}
System.out.println("over");
}
try {
System.out.println(a / b);
System.out.println(arr[10]);
} catch (ArithmeticException | ArrayIndexOutOfBoundsException e) {
System.out.println("出错了");
}
编译期异常和运行期异常
Throwable的几个常见方法
public class Demo5_Throwable {
public static void main(String[] args) {
try {
System.out.println(1 / 0);
} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println(e);
e.printStackTrace();
}
}
}
throws方式处理异常
package com.heima.exception;
public class Demo6_Exception {
public static void main(String[] args) throws Exception {
Person p = new Person();
p.setAge(-17);
System.out.println(p.getAge());
}
}
class Person {
private String name;
private int age;
public Person() {
super();
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) throws AgeOutOfBoundsException {
if(age >0 && age <= 150) {
this.age = age;
}else {
throw new Exception("年龄非法");
}
}
}
throws和throw的区别
A:throw的概述
*在功能方法内部出现某种情况,程序不能继续运行,需要进行跳转时,就用throw把异常对象抛出;
B:throws和throw的区别
*a:throws
*用在方法声明后面,跟的是异常类名
*可以跟多个异常类名,用逗号隔开
*表示抛出异常,由该方法的调用者来处理
*b:throw
*用在方法体内,跟的是异常对象名;
*只能抛出一个异常对象名;
*表示抛出异常,由方法体内的语句处理
finally关键字的特点及概述
public static void main(String[] args) {
try {
System.out.println(10/0);
} catch (Exception e) {
System.out.println("除数为零了");
System.exit(0);
return;
} finally {
System.out.println("看看我执行了吗");
}
}
finally关键字的面试题
public class Test1 {
public static void main(String[] args) {
Demo d = new Demo();
System.out.println(d.method());
}
}
class Demo {
public int method() {
int x = 10;
try {
x = 20;
System.out.println(1 / 0);
return x;
} catch (Exception e) {
x = 30;
return x;
} finally {
x = 40;
}
}
}
自定义异常
class AgeOutOfBoundsException extends Exception {
public AgeOutOfBoundsException() {
super();
}
public AgeOutOfBoundsException(String message) {
super(message);
}
}
public void setAge(int age) throws AgeOutOfBoundsException {
if(age >0 && age <= 150) {
this.age = age;
}else {
throw new AgeOutOfBoundsException("年龄非法");
}
}
异常的注意事项及如何处理异常
A:异常注意事项
*a:子类重写父类方法时,子类的方法必须抛出相同的异常或者父类异常的子类
*b:如果父类抛出多个异常,子类重写父类时,只能抛出相同的异常或者是他的子集,子类不能抛出父类没有的异常
*c:如果被重写的方法没有异常出现,那么子类的方法绝对不可以抛出异常,如果子类方法内有异常出现,那么子类只能用try,不能用throws
B:如何使用异常处理
*原则:如果该功能内部可以问题处理,用try,如果处理不了,交由调用者处理,这是用throws
*区别:
*后续程序需要继续运行就用try
*后续程序不需要运行就用throws
练习
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个整数:");
while(true) {
String line = sc.nextLine();
try {
int num = Integer.parseInt(line);
System.out.println(Integer.toBinaryString(num));
break;
}catch(Exception e) {
try {
new BigInteger(line);
System.out.println("录入错误,您录入的是一个过大整数,请重新输入一个整数:");
}catch (Exception e2) {
try {
new BigDecimal(line);
System.out.println("录入错误,您录入的是一个小数,请重新输入一个整数:");
} catch (Exception e1) {
System.out.println("录入错误,您录入的是非法字符,请重新输入一个整数:");
}
}
}
}
}
}
File类
File类的概述和构造方法
A:File类的概述
*File更应该叫做一个路径;
*文件路径或者文件夹路径
*路径分为绝对路径和相对路径
*绝对路径是一个固定的路径,从盘符开始;
*相对路径相对于某个位置,在eclipse下是指当前项目下,在dos指的是当前路径
*查看API
*文件和目录路径名的抽象表示形式
B:构造方法
*File(String pathname):根据一个路径得到File对象
*File(String parent, String child):根据一个目录和一个子文件/目录得到File对象
*File(File parent, String child):根据一个父File对象和一个子文件/目录得到File对象
C:案例演示
public static void demo1() {
File file = new File("G:\\自学进度\\java基础\\day01\\Day01.md");
System.out.println(file.exists());
}
public static void demo2() {
String parent = "G:\\自学进度\\java基础\\day01";
String child = "Day01.md";
File file = new File(parent, child);
System.out.println(file.exists());
}
public static void main(String[] args) {
File parent = new File("G:\\自学进度\\java基础\\day01");
String child = "Day01.md";
File file = new File(parent, child);
System.out.println(file.exists());
System.out.println(parent.exists());
}
File类里面的功能
创建功能
**
* * A:创建功能
* public boolean createNewFile():创建文件 如果存在这样的文件,就不创建了
* public boolean mkdir():创建文件夹 如果存在这样的文件夹,就不创建了
* public boolean mkdirs():创建文件夹,如果父文件夹不存在,会帮你创建出来
* B:案例演示
* File类的创建功能
* 注意事项:
* 如果你创建文件或者文件夹忘了写盘符路径,那么,默认在项目路径下。
* @throws IOException
*/
public static void demo1() throws IOException {
File file = new File("yyy.txt");
System.out.println(file.createNewFile());
File file2 = new File("zzz");
System.out.println(file2.createNewFile());
}
public static void main(String[] args) throws IOException {
demo1();
File dir1 = new File("aaa");
System.out.println(dir1.mkdir());
File dir2 = new File("bbb.txt");
System.out.println(dir2.mkdir());
File dir3 = new File("ccc\\ddd");
System.out.println(dir3.mkdirs());
}
重命名和删除功能
判断功能
public static void demo1() {
File dir1 = new File("ccc");
System.out.println(dir1.isDirectory());
File dir2 = new File("zzz");
System.out.println(dir2.isDirectory());
System.out.println(dir1.isFile());
System.out.println(dir2.isFile());
}
public static void main(String[] args) {
File file = new File("zzz");
file.setReadable(false);
System.out.println(file.canRead());
file.setWritable(true);
System.out.println(file.canWrite());
File file2 = new File("ccc.txt");
System.out.println(file2.isHidden());
System.out.println(file.isHidden());
}
获取功能
public static void demo1() {
File file1 = new File("ccc.txt");
File file2 = new File("I:\\百度云下载\\code\\day19\\day19\\ccc.txt");
System.out.println(file1.getAbsolutePath());
System.out.println(file2.getAbsolutePath());
System.out.println(file1.getPath());
System.out.println(file2.getPath());
System.out.println(file1.getName());
System.out.println(file2.getName());
System.out.println(file1.length());
Date d = new Date(file1.lastModified());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
System.out.println(sdf.format(d));
}
public static void main(String[] args) {
File dir = new File("G:\\自学进度\\java基础\\day01");
String[] arr = dir.list();
for (String string : arr) {
System.out.println(string);
}
File[] subFiles = dir.listFiles();
for (File file : subFiles) {
System.out.println(file);
}
}
输出指定目录下指定后缀的文件名
/**
* * A:案例演示
* 需求:判断E盘目录下是否有后缀名为.jpg的文件,如果有,就输出该文件名称
*/
public static void main(String[] args) {
File dir = new File("E:\\");
String[] arr = dir.list(); //获取e盘下所有的文件或文件夹
for (String string : arr) {
if(string.endsWith(".jpg")) {
System.out.println(string);
}
}
/*File[] subFiles = dir.listFiles(); //获取e盘下所有的文件或文件夹对象
for (File subFile : subFiles) {
if(subFile.isFile() && subFile.getName().endsWith(".jpg")) {
System.out.println(subFile);
}
}*/
文件名称过滤器的概述及其使用
A:文件名称过滤器的概述
*public String[] list(FilenameFilter filter)
*public File[] listFiles(FileFilter filter)
B:文件名称过滤器的使用
*需求:判断E盘目录下是否有后缀名为.docx的文件,如果有就输出文件名称;
C:原码分析
*帮文件名称过滤器的list()方法的源码
String[] arr = dir.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
File file = new File(dir, name);
return file.isFile() && file.getName().endsWith(".docx");
}
});
for (String string : arr) {
System.out.println(string);
}