异常处理(接上)
异常的分类
1.运行时异常(程序员犯的错误 代码写错了 比如越界)
2.编译时异常(除了运行时异常 全是编译时异常)
为你可能发生的异常进行一个准备
特点:系统强制你去处理这个异常(不处理就报错)
public class Demo01 {
public static void main(String[] args) throws Exception {
//读取文件 传入要读取文件的路径
//系统不知道你到底有没有这个文件
//强制出一下 没这个文件怎么办?
//相当于 要为可能发生的异常做提前准备
//自己处理 编译时异常
//try {
// FileInputStream fis=new FileInputStream("wl.txt");
//} catch (FileNotFoundException e) {
// System.out.println("你没有这个文件");
//}
//抛出去处理
FileInputStream fis=new FileInputStream("wl.txt");
}
}
编译时异常和运行时异常
需求:写一个类
1.抛出运行时异常的方法
2.抛出编译时异常的方法
3.计算圆面积的方法
public class Demo02 {
public static void main(String[] args) {
Test test=new Test();
//test.fun2();
double area = test.getArea(-5);
System.out.println(area);
}
}
class Test{
//抛编译时异常
public void fun1() throws Exception {
throw new Exception("我是编译时异常");
}
//抛运行时异常
//当你这个方法抛的是运行时异常时 方法上可以省略
//throws RuntimeException
public void fun2() {
throw new RuntimeException("我是运行时异常");
}
//计算圆面积
public double getArea(int r) {
if (r<=0) {
//半径小于0 再向下计算没有意义
//希望程序停下来 让程序员去修改代码
//可以抛出一个异常 运行时异常
throw new RuntimeException("半径小于0");
}
return 3.14*r*r;
}
}
继承中的异常
public class Demo03 {
}
class Father{
//如果父类抛出了异常
//子类可以抛出异常 也可以try---catch处理
public void fun() {
}
}
class Son extends Father{
@Override
public void fun() {
//调用带异常的方法
//问 能不能把异常抛出去处理
//这时只能用try---catch处理
try {
//当父类中的方法没有抛出异常
//那么子类在重写父类的这个方法的时候
//也不能抛出异常
method();
} catch (Exception e) {
}
}
//写一个抛出异常的方法
public void method() throws Exception {
throw new Exception();
}
}
file
与系统有关的路径分隔符(pathseparator)与系统有关的默认名称分隔符(separator)
private static void fun1() {
String pathseparator = File.pathSeparator;
System.out.println(pathseparator);
String separator = File.separator;
System.out.println(separator);
}
绝对路径
该文件在你当前硬盘上的路径唯一的
/Users/lanou/Desktop/Test
以斜杠开头是绝对路径
相对路径(不以斜杠开头)
必须要有参照物
参照物是你工程
private static void fun2() {
File file1=new File("/Users/lanou/Desktop/Test");
//判断该文件是否存在
boolean b1 = file1.exists();
System.out.println(b1);
//打印一下绝对路径
System.out.println(file1.getAbsolutePath());
//利用父子路径创建File对象
File file2=new File("/Users/lanou/Desktop","Test");
boolean b2= file2.exists();
System.out.println(b2);
//利用一个文件和一个路径创建File对象
File parent=new File("/Users/lanou/Desktop");
String child="Test";
File file3=new File(parent, child);
boolean b3 = file3.exists();
System.out.println(b3);
}
创建方法与创建文件
private static void fun3() throws IOException {
File file1=new File("/Users/lanou/Desktop/Test/wl.txt");
//该方法不会重复创建文件(只创建一次)
//只能创建文件(没后缀 也是文件)
boolean b1 = file1.createNewFile();
System.out.println(b1);
//获取文件名方法
System.out.println(file1.getName());
//toString 打印的是文件绝对路径
System.out.println(file1);
//创建文件夹
File file2=new File("/Users/lanou/Desktop/Test/a/b");
//该方法不能创建多级文件夹
boolean b2 = file2.mkdir();
System.out.println(b2);
//创建多级文件夹
File file3=new File("/Users/lanou/Desktop/Test/c");
//该方法不能创建多级文件夹
boolean b3 = file3.mkdirs();
System.out.println(b3);
}
判断、判断是否存在、判断是文件
private static void fun4() {
File file1=new File("/Users/lanou/Desktop/Test/wl.txt");
boolean b1 = file1.isFile();
System.out.println(b1);
//判断是文件夹
boolean b2 = file1.isDirectory();
System.out.println(b2);
//判断是否是隐藏文件
boolean b3 = file1.isHidden();
System.out.println(b3);
}
删除方法(慎重 不会经过你回收站)
private static void fun5() {
//删除文件夹时 文件夹内部不能有文件
File file1=new File("/Users/lanou/Desktop/Test/c");
boolean b1 = file1.delete();
System.out.println(b1);
}
文件遍历
private static void fun6() {
File file1=new File("/Users/lanou/Desktop/Test");
//获取文件夹下的所有文件 以字符串数组形式
//获取的是文件名(一级文件名)
String[] list = file1.list();
for (String string : list) {
System.out.println(string);
}
//获取文件夹下的所有文件 以文件数组形式
File[] listFiles = file1.listFiles();
for (File file : listFiles) {
System.out.println(file);
}
}
例题
需求:遍历打印Test文件夹下所有文件 封装一个方法(递归)
public static void printFile(File file) {
File[] files = file.listFiles();
for (File f : files) {
//判断 如果不是文件夹 直接打印
if (f.isDirectory()) {
//是文件夹 继续遍历
printFile(f);
}else {
//是文件
System.out.println(f);
}
}
}
public static void main(String[] args) {
File file1=new File("/Users/lanou/Desktop/Test");
printFile(file1);
}