2020-11-19 try-catch-finally和try-with-resources

参考


使用try-catch-finally读取文件并输出其内容

package com.yimin;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

public class App {
    public static void main(String[] args) {
        // D:/Projects/VSCodeProjects/Java/test-maven/target/classes/%e7%89%9b%e9%80%bc.txt
        String file = App.class.getResource("/牛逼.txt").getFile();
        try {
            // 文件名包含中文,需要编码
            String path = URLDecoder.decode(file, "utf-8");
            reader(path);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    public static void reader(String path) {
        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new FileReader(path));
            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                System.out.println(str);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(bufferedReader!=null){
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
项目结构

要读取的文件【牛逼.txt】包含中文,必须要使用URLDecoder.decode(file, "utf-8");进行编码,否则会抛出java.io.FileNotFoundException异常


使用try-with-resources

package com.yimin;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

public class App2 {

    public static void main(String[] args) {
        // D:/Projects/VSCodeProjects/Java/test-maven/target/classes/%e7%89%9b%e9%80%bc.txt
        String file = App.class.getResource("/牛逼.txt").getFile();
        try {
            // 文件名包含中文,需要编码
            String path = URLDecoder.decode(file, "utf-8");
            reader(path);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    public static void reader(String path) {
        try (BufferedReader bufferedReader = new BufferedReader(new FileReader(path)) {
            @Override
            public void close() throws IOException {
                super.close();
                System.out.println("close方法被调用");
            }
        };) {
            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                System.out.println(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

运行结果

牛逼
牛逼
牛逼
close方法被调用

方法中并没有使用finally语句块手动关闭资源,而是自动关闭了,是因为BufferedReader 实现了AutoCloseable接口,只要需要释放的资源实现了AutoCloseable接口,使用try-with-resources的方式(即写在try后的()中),就会自动调用close方法


注:BufferedReader的继承关系如下

public class BufferedReader extends java.io.Reader
public abstract class Reader implements java.lang.Readable, java.io.Closeable
public abstract interface Closeable extends java.lang.AutoCloseable
public abstract interface AutoCloseable

你可能感兴趣的:(2020-11-19 try-catch-finally和try-with-resources)