黑马程序员 <a href="http://www.itheima.com" target="blank">java培训</a>

   第十九天笔记

1.异常的概述和分类

异常就是程序在运行中出现的一些问些,在开发过程中必须要处理的.

异常的分类:

 Throwable类是Java中所有错误或异常的超类。

错误:Error

异常:Exception

Java中异常的分类

编译异常:

运行异常:在运行阶段会出现的问题。

public class ExceptionDemo1 {

    public static void main(String[] args) {
        // int a = 10;
        // int b = 0;
        // System.out.println(a / b); // java.lang.ArithmeticException ----运行时异常
        //
        // SimpleDateFormat sdf = new SimpleDateFormat();

        // sdf.parse("asdfsadf"); //就是一个编译异常,要求我们程序员必须对异常进行处理.

        // int[] arr={1,2,3};

        // System.out.println(arr[3]);  //数组下标越界   ArrayIndexOutOfBoundsException
        //int n = Integer.parseInt("abc"); //NumberFormatException
        //System.out.println(n); 
        
        //空指针
        String s=null;
        
        System.out.println(s.length());
        System.out.println("hello world");
    }
}

 

2.Jvm默认怎样处理异常

注意:在java中,它已经定义好了一些问题的描述.

例如:数组下标越界 ArrayIndexOutOfBoundsException

将一个字符串转换成数字  有可能出现  NumberFormatException

总结:jvm会将问题包装成具体的异常类对象,并通过这个对象调用它的一些方法,来在控制台上把问题反馈。

3.Try….catch的方式处理异常

异常分类:

                   运行时异常:RuntimeException及其子类

                   编译异常:Exception其及子类,但是不包含RuntimeException及其子类

对于异常,运行时异常可以处理也可以不处理,编译异常必须处理。

try…catch格式:

try{

         有可能出现异常的代码块;

}catch(异常类型  e){

         处理方式

}

运行的代码如果出现的异常,catch会捕获到,你在catch中要捕获的是什么异常,如果出现了,就会将其捕获.

 

public class ExceptionDemo2 {

    public static void main(String[] args) {
        
        //处理除数为0异常
        
        int a=10;
        int b=10;
        //System.out.println(a/b);  //java.lang.ArithmeticException  运行时异常
        
        //现在我们要使用try...catch来处理异常.
        
        try {
            System.out.println(a / b); // 有可能出现异常
        } catch (ArithmeticException e) { //catch只有在try块中的代码出现了异常才会执行.
            System.out.println("除数不能为0");
        }
    }
}

 

4.编译期异常和运行异常的区别

编译异常,要求我们必须处理,不处理编译不了代码,运行时异常,你可以处理,也可以不处理,不处理jvm处理。

编译异常  Exception类及其子类,除了RuntimeException及其子类,运行异常  RuntimeException及其子类。

5.Throws的方式处理异常

关于java中异常处理方式

  1. try…catch
  2. throws

throws处理的格式:

         我们在方法后面直接写  throws  异常类,这就代表要将这个异常抛出。

//throw作用

public class ExceptionDemo8 {
    public static void main(String[] args) {
        try {

            show();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public static void show() throws Exception {
        int a = 10;
        int b = 0;
        // 对除数进行一个判断
        if (b == 0) {
            throw new Exception("除数不能为0"); // throw是用于抛出具体问题
        }
        System.out.println(a / b);

    }
}
//在方法上是否使用throws,取决于方法内的throw抛出的异常类型,如果抛出的是编译异常,那么在方法上必须通过throws声明,而throw如果是一个运行异常,那么
//在方法上可以throws异常,也可以不用。

 

6.throw的概述以及和throws的区别

throw是用于抛出具体异常的,注意,它抛出的是异常对象。

对于throw,如果执行了,方法体也就结束 了。

在开发中经常会使用throw让我们的方法结束。

 

7.finally关键字的特点及作用

在实际开发中,我们使用try..catch一般都 会与finally连用。

格式

try{

}catch(类型 e){

 

}finally{

}

//finally使用

public class ExceptionDemo9 {
    public static void main(String[] args) {

        int a = 10;
        int b = 0;
        try {
            System.out.println(a / b);
            
            //释放资源
        } catch (ArithmeticException e) {
            System.out.println("除数不能为0");
            //释放资源
            // System.exit(0);如果出现这句话finally是执行不了的。
        } finally {
            System.out.println("finally");
            //释放资源
        }

        // 1.程序出现了异常,执行catch后还要执行finally
        // 2.程序没有出现异常,执行try中内容后,也会执行finally.
        // 总结:finally中的内容永远都会被执行.
        
        //问题:finally有什么作用?
        //在开发中finally中一般有于释放资源,执行关闭动作。
        
    }
}

 

8.关于final finally finalize 的区别

Final是一个修饰符

修饰属性    常量

修饰方法    不可以被重写

修饰类      不可以被继承

 

Finally 异常中的,这个块中的内容永远会被执行,用于关闭资源。

 

Finalize是Object类中的一个方法,用于垃圾回收.

9.异常处理的变形以及自定义异常

关于异常变形

 try…catch..finally

try…catch

Try…catch..catch..catch

try…finally

对于我们开发中,在try,catch,finally中又嵌套了try..catch

自定义异常:

继承自异常类的一个自定义的类,这个类就叫做自定义异常。

自定义异常的作用

在实际开发中,我们会处理很多关于业务操作的问题,而这些问题,是在jdk中的异常无法描述的,那么我们就需要定义一个自定义异常来描述。

10.异常的注意事项

A.如果父类(父接口)的方法没有定义异常,子类(实现类)重写的方法不能抛出异常。

B.如果父类(父接口)的方法定义了异常,子类(实现类)重写的方法可以抛出同样异常,也可以不抛出。

C.如果父类(父接口)的方法定义了异常,子类也要抛出异常,这时,可以抛出与父类相同的异常或者其异常的子集

11.File类的概述和构造方法

   File类就是使用java中提供的File类来映射一个文件。

注意:我们使用File来映射一个文件,只能对文件的属性进行操作,而不能修改文件的内容。如果要对文件的内容进行操作,只能使用IO流。

  1. File的构造方法

File类是在java.io包下.

File(String pathname)

File(String parent,String child)

File(File parent,String child)

 

public class FileDemo1 {

    public static void main(String[] args) {

        // 问题,下面代码是否会有问题?

        File file = new File("d:/b/c/d/a.txt");
    }

    // 1.使用File(String pathname)来描述d:/a.txt文件
    public static void demo1() {
        // File file = new File("d:\\a.txt");

        // File file=new File("d:/a.txt");

        // 注意:在linux系统中它的分隔符就是"/" 在widnows系统中它的分隔符是"\",在java开发中,可以使用一个/来代替\\

        // 注意:如果想要写一个通用的程序,在任何系统上都可以使用
        File file = new File("d:" + File.separator + "a.txt");
    }

    // 2.使用File(String parent,String child)来描述 d:/a.txt文件
    public static void demo2() {
        File file = new File("d:/a", "a.txt");
    }

    // 3.使用File(File parent,String child)来描述d:/a/a.txt
    public static void demo3() {
        File parent = new File("d:/a"); // 它映射的是d:/a目录

        File file = new File(parent, "a.txt"); // d:/a目录下的a.txt文件。
    }
}

 

2.File的创建功能

2.1.创建文件   createNewFile()

public boolean createNewFile(); 这个方法抛出了IOException异常,我们在使用它时需要处理,如果指定的文件的路径错误,抛出异常

2.2.创建目录  mkdir();  public boolean mkdir();

2.3.创建目录mkdirs();  用于创建多层目录 ;public boolean mkdirs();

public class FileDemo2 {

    public static void main(String[] args) {
        demo3();
    }

    // 1.创建文件 createNewFile()
    public static void demo1() {
        File file = new File("d:/a.txt"); // 这个文件不存在

        try {
            System.out.println(file.createNewFile());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    // 2.创建目录
    public static void demo2() {
        File file = new File("d:", "a.txt"); // 这个文件不存在

        file.mkdir();
    }

    // 3.创建多层目录
    public static void demo3() {
        File file = new File("d:/a/b/c");

        System.out.println(file.mkdirs());
    }
}

 

 

3.关于java中文件的路径问题

绝对路径:可以简单理解成是c:/xxx  d:/xxx 带盘符

  相对路径:它是需要参照物的。

  如果我们使用记录本来操作,这时创建的文件与.java在同一目录下。

  如果我们使用eclipse它的相对路径,在当前工和下,它是相对于src。

注意:我们在使用相对路径是时,经常使用”..”,它代表的是上一层目录。

 

3.File文件的功能

A.删除操作

delete方法

public boolean delete()  它可以删除目录也可以删除文件。

java中的File类的delete方法删除时不走回收站。

//删除操作
//delete方法
//public boolean delete()  它可以删除目录也可以删除文件。
//java中的File类的delete方法删除时不走回收站。
public class FileDemo4 {

    public static void main(String[] args) {
        
        
        //File file=new File("d:/a/a.txt");
        
        //删除文件
        //file.delete();
        
        File file=new File("d:/a");
        //删除目录,如果目录中存在了子目录或子文件,目录是不能删除的。
        System.out.println(file.delete()); 
    }
}

 

 B.重命名 

public boolean renameTo(File dest)

如果使用renameTo方法,路径没有改变,直接修改名称,如果路径改变 ,它相当于改名后剪切。

如果使用renameTo方法,对目录操作,可以改名,但不能改变位置.

public class FileDemo5 {

    public static void main(String[] args) {
        //创建一个文件
        File file=new File("d:/a");
        //重命名
        System.out.println(file.renameTo(new File("d:/b")));
    }
}

 

4.File文件的判断功能

A.判断文件或目录是否存在

public boolean exists()  返回值为true,代表存,否则不存在。

B.判断是文件

public boolean isFile()

C.判断是目录

public boolean isDirectory();

D.判断是否是隐藏文件

public boolean isHidden();

E.判断是否是只读

public boolean canRead();

F.判断是否是只写

public boolean canWrite();

5.File的获取功能

A.获取文件的长度

public long length()  获取文件长度,单位是字节(byte)

B.获取当前文件的绝对路径(不规范)

public String getAbsolutePath()

C.获取当前文件的绝对路径(规范)

public String getCanonicalPath()

D.获取当前文件的名称

public String getName();

public class FileDemo7 {

    public static void main(String[] args) throws IOException {
        demo3();
    }
    //获取文件长度
    public static void demo1(){
        File file=new File("c:/a.txt");
        System.out.println(file.length());
        
    }
    
    //获取文件名称
    public static void demo2(){
        File file=new File("c:/a.txt");
        System.out.println(file.getName());
    }
    
    //获取文件的绝对磁盘路径
    public static void demo3() throws IOException{
        File file=new File("c:/a/../a.txt");
        
        System.out.println(file.getAbsolutePath());
        System.out.println(file.getCanonicalPath());
    }
}

 

12.File的高级获取功能

A.list

public String[] list();用于获取当前目录下的所有子的名称的一个字符串数组。

B.listFiles

public File[] listFiles();用于获取当前目录下所有子的一个File数组

 

 

public class FileDemo8 {

    public static void main(String[] args) {
        demo1();
    }

    // list方法
    public static void demo1() {        

        File file = new File("c:/a");
        String[] files = file.list();
        
        for(String f:files){
            System.out.println(f);
        }
    }
    
    // listFiles方法
    public static void demo2() {

        File file = new File("c:/a");  
        File[] files = file.listFiles();
        //new File("c:/a/a.txt")  new File("c:/a/b");
        for(File f:files){
            System.out.println(f.getName()); //获取文件名称
        }
    }
}

 

posted on 2015-04-16 00:22  星之钥匙  阅读(126)  评论(0编辑  收藏  举报