读取resources下的资源

这里我通过一个普通的SpringBoot项目进行测试,当然其他项目也都是通用的。

将其中的Test修改为你的类名即可:

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;


import java.io.*;
import java.net.URLDecoder;
import java.util.Objects;

/**
 * @ClassName Test
 * @Author zhangzhixi
 * @Description
 * @Date 2022-8-11 16:48
 * @Version 1.0
 */
@Slf4j
public class Test {
    public static void main(String[] args) throws IOException {
        log.info("\n==============测试读取resources下的资源==============");
        String fileName = "application.yml";
        log.info("\n==============第一版==============");
        function1(fileName);
        log.info("\n==============第二版==============");
        function2(fileName);
        log.info("\n==============第三版==============");
        function3(fileName);
        log.info("\n==============第四版==============");
        function4(fileName);
        log.info("\n==============第五版==============");
        function5(fileName);
        log.info("\n==============第六版==============");
        function6(fileName);
        log.info("\n==============第七版==============");
        function7(fileName);
    }

    /**
     * 通过类路径找到resources下的资源
     *
     * @param fileName 资源名称
     * @throws IOException 异常
     */
    private static void function1(String fileName) throws IOException {
        String path = Objects.requireNonNull(Test.class.getClassLoader().getResource("")).getPath();
        System.out.println(path);
        String filePath = path + fileName;
        System.out.println(filePath);
        getFileContent(filePath);
    }

    /**
     * 直接通过文件名getPath来获取路径
     *
     * @param fileName 文件名
     * @throws IOException IO异常
     */
    public static void function2(String fileName) throws IOException {
        String path = Objects.requireNonNull(Test.class.getClassLoader().getResource(fileName)).getPath();
        System.out.println(path);
        //如果路径中带有中文会被URLEncoder,因此这里需要解码
        String filePath = URLDecoder.decode(path, "UTF-8");
        System.out.println(filePath);
        getFileContent(filePath);
    }

    /**
     * 直接使用getResourceAsStream方法获取流
     * springboot项目中需要使用此种方法,因为jar包中没有一个实际的路径存放文件
     *
     * @param fileName 文件名
     * @throws IOException IO异常
     */
    public static void function3(String fileName) throws IOException {
        InputStream in = Test.class.getClassLoader().getResourceAsStream(fileName);
        getFileContent(in);
    }

    /**
     * 直接使用getResourceAsStream方法获取流
     * 如果不使用getClassLoader,可以使用getResourceAsStream("/配置测试.txt")直接从resources根路径下获取
     *
     * @param fileName 文件名
     * @throws IOException IO异常
     */
    public static void function4(String fileName) throws IOException {
        InputStream in = Test.class.getResourceAsStream("/" + fileName);
        getFileContent(in);
    }

    /**
     * 通过ClassPathResource类获取,建议SpringBoot中使用
     * springboot项目中需要使用此种方法,因为jar包中没有一个实际的路径存放文件
     *
     * @param fileName 文件名
     * @throws IOException IO异常
     */
    public static void function5(String fileName) throws IOException {
        ClassPathResource classPathResource = new ClassPathResource(fileName);
        InputStream inputStream = classPathResource.getInputStream();
        getFileContent(inputStream);
    }

    /**
     * 通过绝对路径获取项目中文件的位置(不能用于服务器)
     *
     * @param fileName 文件名
     * @throws IOException IO异常
     */
    public static void function6(String fileName) throws IOException {
        // 获取项目的绝对路径
        String rootPath = System.getProperty("user.dir");
        String filePath = rootPath + File.separatorChar + "src" + File.separatorChar + "main" + File.separatorChar + "resources" + File.separatorChar + fileName;
        getFileContent(filePath);
    }

    /**
     * 通过绝对路径获取项目中文件的位置(不能用于服务器)
     *
     * @param fileName 文件名
     * @throws IOException IO异常
     */
    public static void function7(String fileName) throws IOException {
        //参数为空
        File directory = new File("");
        //规范路径:getCanonicalPath() 方法返回绝对路径,会把 ..\ 、.\ 这样的符号解析掉
        String rootCanonicalPath = directory.getCanonicalPath();
        String filePath = rootCanonicalPath + File.separatorChar + "src" + File.separatorChar + "main" + File.separatorChar + "resources" + File.separatorChar + fileName;
        getFileContent(filePath);
    }

    /**
     * 根据文件路径读取文件内容
     *
     * @param fileInPath 文件路径
     */
    public static void getFileContent(Object fileInPath) throws IOException {
        BufferedReader br = null;
        if (fileInPath == null) {
            return;
        }
        if (fileInPath instanceof String) {
            br = new BufferedReader(new FileReader((String) fileInPath));
        } else if (fileInPath instanceof InputStream) {
            br = new BufferedReader(new InputStreamReader((InputStream) fileInPath));
        }
        String line;
        while (true) {
            assert br != null;
            if ((line = br.readLine()) == null) {
                break;
            }
            System.out.println(line);
        }
        br.close();
    }
}
posted @ 2022-08-11 17:28  Java小白的搬砖路  阅读(189)  评论(0编辑  收藏  举报