Java 抛出异常(编译异常与运行时异常)以及ava.io.File包下的File类

一、编译与运行时异常
1.编译异常:系统会强制你去处理(try{}catch{}
2.运行时异常--RuntimeException:
a.抛出运行时异常,在方法的声明上,就可以不用throws来识别(区别编译时异常)
b.可以不用在运行时对异常进行处理
例题
public class Demo01 {
public static void main(String[] args) throws Exception {
    Text text = new Text();
    //这里测试的是编译异常,Text中的fun1方法抛出异常给调用者text,抛出异常
    text.fun1();

    Exception in thread "main" java.lang.Exception
    at com.lanou3g.exception.Text.fun1(Demo01.java:30)
    at com.lanou3g.exception.Demo01.main(Demo01.java:17)

    //测试运行时异常
    text.fun2();
我是运行时异常
Exception in thread "main" java.lang.RuntimeException
    at com.lanou3g.exception.Text.fun2(Demo01.java:35)
    at com.lanou3g.exception.Demo01.main(Demo01.java:19)

    //测试圆面积--这类我们输入-1,在Text里我们进行运行异常处理
    double area = text.getArea(-1);
    System.out.println(area);

Exception in thread "main" java.lang.RuntimeException: 半径不对
    at com.lanou3g.exception.Text.getArea(Demo01.java:42)
    at com.lanou3g.exception.Demo01.main(Demo01.java:22)

}
}
class Text {
    public void fun1() throws Exception {
         throw new Exception();
    }
    public void fun2()  {
        System.out.println("我是运行时异常");
        throw new RuntimeException();
    }
    //计算圆面积
    public double getArea(double r) {
    //对于圆面积的计算,我们都知道输入的半径应该要大于0,不然毫无意义,所以这里我们抛出运行时异常,停止程序,修改代码
        if (r<= 0) {
            throw new RuntimeException("半径不对");

        }
         return r*r*Math.PI;
    }
}
二、继承中方法重写异常
 例题:

  class Father{
  //这里父类fun方法抛出异常了,我们在子类中重写这个方法时,我们可以抛出也可以不抛出异常
  但是,如果父类如果没有进行fun方法抛出异常,那么子类中就一定不能抛出该方法的异常
      public void fun() throw Exception{

      }
  }
  class Son extends Father{
      //重写父类fun方法
      public void fun(){

      }
  }

class Son extends  Father {
    public void fun() {
        try {
            method();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    //这个是Son类中的抛出异常的方法,那么我们如//果在子类的重写父类方法中调用该异常方法,//我们应该怎么在子类的fun方法中进行处理
//  ----我们只能使用try{}catch{}进行处理,不能进行抛出异常处理(因为,父类并没有进行抛出异常处理,子类就不能进行,上面说过了
    public void method() throws Exception {

    }
}
三、File类
1.作用:a.操作文件  b.操作文件夹 c.操作文件的路径

2.构造方法
a.File(String pathname);通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。
b.File(String parent,String child);  根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例。
c.File(File parent,String chile);    根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。
3.方法
a.创建功能
public boolean createNewFile();    当且仅当不存在具有此抽象路径名指定名称的文件时,不可分地创建一个新的空文件。
public boolean mkdir(); 创建此抽象路径名指定的目录。
public boolean mkdirs(); 创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。
b.删除功能
public boolean delete();  删除此抽象路径名表示的文件或目录。
c.判断功能
public boolean isDirectory()//判断是否是文件夹
public boolean isFile();//判断是否是文件
public boolean exists();//判断文件/文件夹是否存在
d.获取功能
public String getPath()//将此抽象路径名转换为一个路径名字符串。
public String getName();// 返回由此抽象路径名表示的文件或目录的名称。
public long length();// 返回由此抽象路径名表示的文件的长度。
public String getAbsolutePath();//  返回此抽象路径名的绝对路径名字符串。
public File getAbsoluteFile();//    返回此抽象路径名的绝对路径名形式。
public File getParentFile();//  返回此抽象路径名父目录的抽象路径名;如果此路径名没有指定父目录,则返回 null。
public String getParent(); 返回此抽象路径名父目录的路径名字符串;如果此路径名没有指定父目录,则返回 nullpublic String [] list();//  返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中的文件和目录。
public File [] listFiles();// 返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件。
4.字段摘要(静态成员变量)
a.static String pathSeparator//  与系统有关的路径分隔符,为了方便,它被表示为一个字符串。
b.static String separator;//  与系统有关的默认名称分隔符,为了方便,它被表示为一个字符串。
例题:

 public class Demo01{
     public static void main(String [] args) throws IOException{

     }
     //静态成员变量
     public static void fun1(){
         String pathseparator = File.pathSeparator;
         System.out.println(pathseparator);
         System.out.println(pathseparator);
注:在不同平台下,所获取的分隔符不同
Mac:路径分隔符是 : 冒号
Windows:分隔符是 ; 分号
/
String separtor = File.separator;
System.out.println(separtor);
Mac:分隔符 /
Window:分隔符 \

/

/构造方法
//相对路径:有参照物
//绝对路径:硬盘上的路径,这个路径唯一
public static void fun3(){
//这里Mac系统下Desktop(桌面)/Test(这是名为Test的文件夹)
    File file = new File ("/Users/lanou/Desktop/Test");
    System.out.println(file);
    结果:/Users/lanou/Desktop/Test
 //判断该路径是否存在
 boolean b1 = file.exists();
 System.out.println(b1);
 结果:true
  //使用相对路径,参照物是当前工程根目录
  File file1 = new File("src/wl.txt");
  boolean b2 = file.exists();
  System.out.println(b2);
  结果:true

   //获取绝对路径
  String absolutePath = file1.getAbsolutePath();
  System.out.println(absolutePath);
  结果:/Users/lanou/eclipse-workspace/sh-day023/src/wl.txt

  //使用构造方法二
  String parent = "/Users/lanou/Desktop";
  String child = "/Test";
  //父路径 和 子路径
  File file2 = new File(parent,child);
  boolean b3 = file2.exists();
  System.out.println(b3);
  结果:true

  //构造方法3
  File f = new File(parent);
  File file3 = new File (f,child);
  System.out.println(file3.exists());
  结果:true
}
//创建文件--方法
public static void fun4(){

    File file = new File("/Users/lanou/Desktop/Test/haha.txt");
    boolean b1 = file.createNewFile();
    System.out.println(b1);
    结果:文件没有创建过--true,反之--false
    File b2 = new File("src/dp.txt");
    boolean b = b2.createNewFile();
    System.out.println(b);
    结果:结果:文件没有创建过--true,反之--false
    注:无论带不带后缀,都是文件
}
 //删除,所删除的东西,不经过垃圾回收站,删除的东西要小心
 public  static void fun5(){
     File file = new File ("/Users/lanou/Desktop/Test/x");
     boolean b1 = file.delete();
     System.out.println(b1);
    结果:有此文件删除--true,反之--false
 }
//判断文件是否存在
 public static void fun6(){
     File file = new File ("/Users/lanou/Desktop/Test/haha.txt");
     boolean b1 = file.isDirectory();
     System.out.println(b1);
   结果:有此文件夹--true,反之--false
   boolean b2 = file.isFile();
   System.out.println(b2);
   结果:有此文件--true,反之--false

 }
 //获取路径,我们所获取的路径,不管是否存在,都会打印所实例的对象路径
 public static void fun7(){
     File file = new File("/Users/lanou/Desktop/Test/www.txt");
     Sytring path = file.getPath();
     System.out.println(path);
     结果:/Users/lanou/Desktop/Test/www.txt
    //获取文件名字
    String name = file.getName();
    System.out.println(name);
    结果:www.txt
    //获取文件所占用的字节
    long length = file.length();
    System.out.println(length);
    结果:这里面,我在www.txt里面输入了一个"wang" 打印是 4个字节
    注:英文--1个字节
        中文--3个字节(这里UTF-8)
        不同编码格式字节数不一样的
//获取父级路径
   File parentFile = file.getParentFile();
   System.out.println(parentFile);
   结果:/Users/lanou/Desktop/Test
 }
  //list与 listFiles方法
  public void fun8(){
      File file = new File ("/Users/lanou/Desktop/Test");
      String [] list = file.list();
      for(String string : file.list){
          System.out..println(string);
        结果:
.DS_Store
g
haha
haha.txt
hahah.txt
www.txt
x
注:这个是我所创建的Test文件夹下所有文件和文件夹的名字,且仅仅能查看一层,.DE-Store是隐藏文件,

  File [] listFiles = file.listFiles();
  for(File file2 :listFiles){
      System.out.println(file2);
      结果
/Users/lanou/Desktop/Test/.DS_Store
/Users/lanou/Desktop/Test/g
/Users/lanou/Desktop/Test/haha
/Users/lanou/Desktop/Test/haha.txt
/Users/lanou/Desktop/Test/hahah.txt
/Users/lanou/Desktop/Test/www.txt
/Users/lanou/Desktop/Test/x
注:listFiles返回的是文件与文件夹的全路径名,是以对象形式
        } 

      }
  //遍历Test下所有的文件夹下所有文件和文件夹
 public static void fun9(){
     File file = new File("/Users/lanou/Desktop/Test");
    File [] listFiles = file.listFiles();
    for(File f : listFiles){
        if(f.isDirectory()){
            getFileName(f);
        }else{
            System.out.println(f);
结果:
/Users/lanou/Desktop/Test/.DS_Store
/Users/lanou/Desktop/Test/haha.txt
/Users/lanou/Desktop/Test/hahah.txt
/Users/lanou/Desktop/Test/www.txt
/Users/lanou/Desktop/Test/x/.DS_Store

/Users/lanou/Desktop/Test/x/y/z/www.txt
        }
    }
  }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值