Java开发之路

文章32 |   阅读 9010 |   点赞0

来源:https://blog.csdn.net/sunnyyoona

[Java开发之路](20)try-with-resource 异常声明

x33g5p2x  于2021-03-13 发布在 其他  
字(3.2k)|赞(0)|评价(0)|浏览(288)

Try-with-resources是java7中一个新的异常处理机制,它能够很容易地关闭在try-catch语句块中使用的资源。

在java7以前,程序中使用的资源需要被明确地关闭,过程有点繁琐,如下所示:

package com.qunar.lectures.tryResource;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by xiaosi on 16-3-4.
 */
public class TryResourceDemo {
    // 获取资源数据
    public static List<String> readLines(String resourcePath) {
        String path = TryResourceDemo.class.getResource(resourcePath).getPath();
        File file = new File(path);

        if (!file.exists()) {
            throw new RuntimeException("Can not find file + " + resourcePath);
        }//if

        if (!file.isFile()) {
            throw new RuntimeException(resourcePath + " is not a regular file");
        }//if

        FileInputStream fis;
        InputStreamReader isr;
        BufferedReader br = null;
        try {
            fis = new FileInputStream(file);
            isr = new InputStreamReader(fis, "UTF-8");
            br = new BufferedReader(isr);
            List<String> lines = new ArrayList<String>();
            String line;
            while ((line = br.readLine()) != null) {
                lines.add(line);
            }//while
            return lines;
        }
        catch (IOException e) {
            throw new RuntimeException("Read file failed , file=" + resourcePath, e);
        }
        finally {
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }//if
        }//finally
    }
    public static void main(String[] args) {
        List<String> lines = readLines("/a.txt");
        for(String line : lines){
            System.out.println("line:" + line);
        }//for
    }
}

假设try语句块抛出一个异常,然后finally语句块被执行。同样假设finally语句块也抛出了一个异常。那么哪个异常会根据调用栈往外传播?即使try语句块中抛出的异常与异常传播更相关,最终还是finally语句块中抛出的异常会根据调用栈向外传播。

private static void printFileJava7() throws IOException {
    try(FileInputStream input = new FileInputStream("file.txt")) {
        int data = input.read();
        while(data != -1){
            System.out.print((char) data);
            data = input.read();
        }
    }
}

我们看到第一行:

try(FileInputStream input = new FileInputStream("file.txt")) {

这就是try-with-resource 结构的用法。FileInputStream 类型变量就在try关键字后面的括号中声明并赋值。在这声明的变量我们可以在下面的代码中直接使用,即同一个作用域中。当try语句块运行结束时,FileInputStream会被自动关闭。这是因为FileInputStream 实现了java中的java.lang.AutoCloseable接口。所有实现了这个接口的类都可以在try-with-resources结构中使用。

当try-with-resources结构中抛出一个异常,同时FileInputStreami被关闭时(调用了其close方法)也抛出一个异常,try-with-resources结构中抛出的异常会向外传播,而FileInputStreami被关闭时抛出的异常被抑制了。这与文章开始处利用旧风格代码的例子(在finally语句块中关闭资源)相反。

在JDK7中只要实现了AutoCloseable或Closeable接口的类或接口,都可以使用try-with-resource来实现异常处理和资源关闭。

你可以在块中使用多个资源而且这些资源都能被自动地关闭:

package com.qunar.lectures.tryResource;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by xiaosi on 16-3-4.
 */
public class TryResourceDemo {
    // 获取资源数据
    public static List<String> readLines(String resourcePath) {
        String path = TryResourceDemo.class.getResource(resourcePath).getPath();
        File file = new File(path);

        if (!file.exists()) {
            throw new RuntimeException("Can not find file + " + resourcePath);
        }//if

        if (!file.isFile()) {
            throw new RuntimeException(resourcePath + " is not a regular file");
        }//if
        // try-with-resource方式 自动释放资源
        try (FileInputStream fis = new FileInputStream(file);
             InputStreamReader isr = new InputStreamReader(fis);
                BufferedReader br = new BufferedReader(isr)){
            List<String> lines = new ArrayList<String>();
            String line;
            while ((line = br.readLine()) != null) {
                lines.add(line);
            }//while
            return lines;
        }
        catch (IOException e) {
            throw new RuntimeException("Read file failed , file=" + resourcePath, e);
        }
    }
    public static void main(String[] args) {
        List<String> lines = readLines("/a.txt");
        for(String line : lines){
            System.out.println("line:" + line);
        }//for
    }
}

相关文章