Spring boot 从resources文件夹中读取文件

位置:首页>文章>详情   分类: 教程分享 > Java教程   阅读(823)   2023-03-28 11:29:14
学习使用ClassPathResourceResourceLoader从 Spring Boot应用程序中的资源文件夹中读取文件
出于演示目的,我data.txt在资源文件夹中添加了具有以下文本内容的文件。
leftso.com
Java Tutorials Blog

1. 类路径资源

ClassPathResource是Resource类路径资源的实现。
它支持解析,java.io.File好像类路径资源驻留在文件系统中,但不支持 JAR 中的资源。要读取 jar 或 war 文件中的文件,请使用resource.getInputStream()方法。

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.FileCopyUtils; 
 
@SpringBootApplication
public class Application implements CommandLineRunner
{
    final Logger LOGGER = LoggerFactory.getLogger(getClass());
             
    public static void main(String[] args) 
    {
        SpringApplication app = new SpringApplication(Application.class);
        app.run(args);
    }
 
    @Override
    public void run(String... args) throws Exception 
    {
        Resource resource = new ClassPathResource("data.txt");
        InputStream inputStream = resource.getInputStream();
        try {
            byte[] bdata = FileCopyUtils.copyToByteArray(inputStream);
            String data = new String(bdata, StandardCharsets.UTF_8);
            LOGGER.info(data);
        } catch (IOException e) {
            LOGGER.error("IOException", e);
        }
    }
}

特别提醒:以下为错误写法,不能写classpath:否则找不到文件。
Resource resource = new ClassPathResource("classpath:data.txt");

 

Console

$ java -jar target\springbootdemo-0.0.1-SNAPSHOT.jar
 
leftso.com
Java Tutorials Blog

2.使用ResourceLoader从资源中读取文件

除了使用ClassPathResource,我们还可以使用ResourceLoader来加载资源(例如类路径或文件系统资源)。
一个ApplicationContext需要提供这样的功能,再加上扩展ResourcePatternResolver支持。
文件路径可以是完全限定的 URL,例如"file:C:/test.dat""classpath:test.dat"。它支持相对文件路径,例如"WEB-INF/test.dat".

$title(​​​​​​​Application.java)
final Logger LOGGER = LoggerFactory.getLogger(getClass());
     
@Autowired
ResourceLoader resourceLoader;
 
@Override
public void run(String... args) throws Exception 
{
    Resource resource = resourceLoader.getResource("classpath:data.txt");
    InputStream inputStream = resource.getInputStream();
 
    try
    {
        byte[] bdata = FileCopyUtils.copyToByteArray(inputStream);
        String data = new String(bdata, StandardCharsets.UTF_8);
        LOGGER.info(data);
    } 
    catch (IOException e) 
    {
        LOGGER.error("IOException", e);
    }
}

Console
 

$ java -jar target\springbootdemo-0.0.1-SNAPSHOT.jar
 
leftso.com
Java Tutorials Blog

 

地址:https://www.leftso.com/article/877.html

相关阅读

学习使用ClassPathResource和ResourceLoader类从 Spring Boot应用程序中的资源文件夹中读取文件
Spring Boot 2.0 绑定properties属性资源文件 Spring Boot 2.0 读取properties配置文件值 Spring Boot 2.0获取properties配...
引言    通过之前spring boot mybatis 整合的讲解: spring boot mybaties整合  (spring boot mybaties 整合 基于Java注解方式写...
Spring Boot 2.0 Redis整合,通过spring boot 2.0整合Redis作为spring缓存框架的实现。
Spring Boot 配置映射本地资源访问注意配置为两个spring.mvc.static-path-pattern= spring.resources.static-locations=这两...
spring boot 1.5整合redis实现spring的缓存框架,spring boot,redis
spring boot又一个spring框架的经典项目,本文讲解spring boot入门的环境配置以及第一个项目,Spring Boot 入门教程
spring boot mybatis 整合使用讲解介绍,spring boot与mybaties的使用讲解介绍。spring boot mybatis xml mapper方式的入门和通过一个...
spring boot 开发技巧,在开发web项目中跳过thyemeleaf模板/js/css等缓存避免每次修改资源文件都需要重启服务器
spring boot 导入本地jar包spring boot maven 打war包时候导入本地jar包