【SpringBoot】三种方式,教你读取 jar 包中的 resources 目录下的文件

读取 resources 目录下的文件路径时,需要注意一点:在本地开发时,我们是能够通过代码获取到这个文件的绝对路径的(如:在 c 盘下或者 d 盘下的);但部署后,项目是通过打成 jar 包运行的,里面的文件是没有实际路径的(只有相对于项目名的相对路径)。

因为最后肯定是打包部署的,所以掌握针对后者的这种方式来读取文件是很有必要的。

代码图如下:
在这里插入图片描述

方式一:(重要)

通过 T.class.getClassLoader().getResourceAsStream() 方法。如:我要读取 config 文件夹下的 test.properties 文件:

这是一个公共方法,用来读取文件中的内容的方法,下面就不再重复了:

public static void printFileContent(Object obj) throws IOException {
    if (null == obj) {
        throw new RuntimeException("参数为空");
    }
    BufferedReader reader = null;
    // 如果是文件路径
    if (obj instanceof String) {
        reader = new BufferedReader(new FileReader(new File((String) obj)));
    // 如果是文件输入流
    } else if (obj instanceof InputStream) {
        reader = new BufferedReader(new InputStreamReader((InputStream) obj));
    }
    String line = null;
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }
    reader.close();
}

读取方法:

public class ResourceUtil {

    public void getResource(String fileName) throws IOException{
        InputStream in = this.getClass().getClassLoader().getResourceAsStream(fileName);
        printFileContent(in);
    }

    public static void main(String[] args) throws IOException {
        new ResourceUtil().getResource("config/test.properties");
    }

}

即使是一个 jar 包,也依旧能读取到。

此方法默认是从 classpath 路径(即:src 或 resources 路径下)下查找文件的,所以,路径前不需要加 “/”。

方式二:(重要)

通过 T.class..getResourceAsStream() 方法。

public void getResource2(String fileName) throws IOException{
    InputStream in = this.getClass().getResourceAsStream("/" + fileName);
    printFileContent(in);
}

public static void main(String[] args) throws IOException {
    new ResourceUtil().getResource2("config/test.properties");
}

此方法默认也是从 classpath 路径(即:src 或 resources 路径下)下查找文件的,但它的路径前为什么需要加 “/” 呢?

这个是跟要读取的文件与当前.class 文件的位置有关。

看看编译后的文件路径:

在这里插入图片描述
当前文件 ResourceUtil.class 与要加载的文件 test.properties 的位置如上:
很显然 test.propertiesResourceUtil.class 不在同一个文件夹下。

那读取的时候是要带上相对路径的,那么,这会有两种情况:

  1. 相对于当前类 ResourceUtil,路径前是不需要加 “/”
  2. 相对于项目名(即:编译后的 classes 文件夹),路径前是需要加 “/”

举例:

  1. 如果 test.properties 和 ResourceUtil 在同一个文件夹下,那么:this.getClass().getResourceAsStream(“test.properties”)
  2. 如果 test.properties 和 ResourceUtil 不在同一个文件夹下,那么:this.getClass().getResourceAsStream(“/config/test.properties”)

如果测试,不要在源文件下添加配置文件,因为编译后,在相应的路径下看不见此配置文件。可以使用 Test.java 代替。

即使是一个 jar 包,也依旧能读取到。

方式三:(重要)

通过 ClassPathResource 方法:

public void getResource3(String fileName) throws IOException{
    ClassPathResource classPathResource = new ClassPathResource(fileName);
    printFileContent(classPathResource.getInputStream());
}

public static void main(String[] args) throws IOException {
    new ResourceUtil().getResource3("config/test.properties");
}

path 前加不加 “/” 无所谓。

即使是一个 jar 包,也依旧能读取到。

  • 16
    点赞
  • 80
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Spring Boot是一个非常流行的Java框架,它可以轻松地将Java应用部署为独立的、自包含的可执行jar文件,包含了应用所有所需的依赖库。但是,有时候我们需要从可执行jar文件读取一些配置文件或者资源文件,这些文件在构建过程中被打包进了jar包文件中。那么该怎么读取这些文件呢? Spring Boot提供了一个非常简单的方式,只需要使用Spring框架中的Resource类就可以轻松地实现。在Spring Boot中使用Resource类来读取jar包中的文件,如下所示: ```java import org.springframework.core.io.Resource; import org.springframework.core.io.ClassPathResource; public class ReadFileFromJar { public static void main(String[] args) throws IOException { Resource resource = new ClassPathResource("config.properties"); InputStream inputStream = resource.getInputStream(); Properties properties = new Properties(); properties.load(inputStream); String value = properties.getProperty("key"); System.out.println(value); } } ``` 上面的代码中,我们首先使用ClassPathResource类创建一个Resource对象,然后调用getInputStream()方法获取文件的输入流。最后,我们使用Properties类读取属性文件的内容,并输出其中的一个属性值。 注意:在使用Spring Boot的时候,所有的资源文件都需要放到classpath路径下,这样Resource类才能正确读取。在IDE集成开发环境中,可以将这些文件放到src/main/resources目录下,或者配置maven的build过程将这些资源文件打包到jar文件中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值