SpringBoot中获取Resource目录下文件

文章展示了在Java中如何使用不同的方法从jar包内部读取资源文件,包括通过getClass().getResourceAsStream(),getClass().getClassLoader().getResourceAsStream(),ClassPathResource以及系统属性和工具类等方式。并给出了每种方法的执行结果。
摘要由CSDN通过智能技术生成

注:在jar包中直接使用路径方式直接读Resource目录下的文件比较难,建议使用流的方式读文件。

1 工程目录

在这里插入图片描述

2 源代码


import org.springframework.boot.system.ApplicationHome;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ResourceUtils;

import java.io.*;

public class TestController {

    public String readFile() throws IOException {

        String path = "config/data";
        System.out.println("(一) 读取jar包文件中的数据流");

        // (一) 读取jar包文件中的数据流

        // (1) 使用getClass()取得当前对象所属的Class对象,注意需要再path前面加”/“
        // 以’/'开头是从项目的ClassPath根下获取资源,不以’/‘开头是从此类所在的包下获取资源。
        InputStream classIS = this.getClass().getResourceAsStream("/"+path);
        System.out.print("1 getClass:");
        System.out.println(classIS);

        // (2) 使用getClassLoader()获得该Class对象的类装载器
        InputStream classLoaderIS = this.getClass().getClassLoader().getResourceAsStream(path);
        System.out.print("2 getClassLoader:");
        System.out.println(classLoaderIS);

        // (3) 使用classPathResource
        ClassPathResource classPathResource = new ClassPathResource(path);
        InputStream classPathIS= classPathResource.getInputStream();
        System.out.print("3 classPathResource:");
        System.out.println(classPathIS);

        System.out.println("");


        // (二) 读取文件路径
        System.out.println("(二) 读取文件路径");

        // (1) 读取jar包同级别目录的文件
        // 获取jar包所在目录
        String jarDir = System.getProperty("user.dir");
        String jarPath = jarDir + "/" + path;
        System.out.println("1 System.getProperty: " + jarPath);
        this.readFile(jarPath);


        // !!! 注意下面的方法,在本地可以运行,在jar包中无法访问到数据文件
        // (2) 使用ResourceUtils读取工程中的文件
        String resourcePath = ResourceUtils.getURL("classpath:").getPath();
        System.out.println("2 ResourceUtils: " + resourcePath+path);
        this.readFile(resourcePath+path);

        // (3) 使用ApplicationHome读取工程中的文件
        ApplicationHome home = new ApplicationHome(this.getClass());
        String appResourcePath = home.getSource().getPath();
        System.out.println("3 ApplicationHome: "+appResourcePath+"/"+path);
        this.readFile(appResourcePath+"/"+path);

        // (4) 使用getClass读取工程中的文件
        String classPath = this.getClass().getResource("/").getPath();
        System.out.println("4 getClass: "+classPath+path);
        this.readFile(classPath+path);

        // (5) 使用getClassLoader读取工程中的文件
        String classLoaderPath = this.getClass().getClassLoader().getResource("").getPath();
        System.out.println("5 getClassLoader: "+classLoaderPath+path);
        this.readFile(classLoaderPath+"/"+path);

        return "Hello";
    }


    public void readFile(String path){
        try {
            // 按行读文件
            BufferedReader reader = new BufferedReader(new FileReader(path));
            String line = reader.readLine();
            while (line != null) {
                System.out.println(line);
                // read next line
                line = reader.readLine();
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) throws IOException {
        TestController test = new TestController();
        test.readFile();
    }
}

3 执行结果

(一) 读取jar包文件中的数据流
1 getClass:java.io.BufferedInputStream@b97c004
2 getClassLoader:java.io.BufferedInputStream@4590c9c3
3 classPathResource:java.io.BufferedInputStream@55f3ddb1

(二) 读取文件路径
1 System.getProperty: D:\1_projects\idea\test/config/data
1111111
2 ResourceUtils: /D:/1_projects/idea/test/target/classes/config/data
1111111
3 ApplicationHome: D:\1_projects\idea\test\target\classes/config/data
1111111
4 getClass: /D:/1_projects/idea/test/target/classes/config/data
1111111
5 getClassLoader: /D:/1_projects/idea/test/target/classes/config/data
1111111
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值