当前位置: 首页 > article >正文

SpringBoot读取类路径下文件

在Spring Boot项目中,经常需要读取resources目录下的文件,不论是配置文件、静态资源还是其他数据文件。本文将介绍9种不同的方式,帮助您轻松读取resources目录下的文件。

方式1: 使用ClassPathResource

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import java.io.InputStream;

public class ResourceExample {
    public static void main(String[] args) throws IOException {
        Resource resource = new ClassPathResource("my-file.txt");
        InputStream inputStream = resource.getInputStream();

        // 进一步处理 inputStream
    }
}

方式2: 使用ResourceLoader

import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.Resource;
import java.io.InputStream;
import org.springframework.beans.factory.annotation.Autowired;

public class ResourceLoaderExample {
    @Autowired
    private ResourceLoader resourceLoader;

    public void readFile() throws IOException {
        Resource resource = resourceLoader.getResource("classpath:my-file.txt");
        InputStream inputStream = resource.getInputStream();

        // 进一步处理 inputStream
    }
}

方式3: 使用ClassLoader

import java.io.InputStream;

public class ClassLoaderExample {
    public static void main(String[] args) throws IOException {
        ClassLoader classLoader = ClassLoaderExample.class.getClassLoader();
        InputStream inputStream = classLoader.getResourceAsStream("my-file.txt");

        // 进一步处理 inputStream
    }
}

方式4: 使用FileCopyUtils

 import org.springframework.util.FileCopyUtils;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

public class FileCopyUtilsExample {
    public static void main(String[] args) throws IOException {
        InputStream inputStream = FileCopyUtilsExample.class.getResourceAsStream("/my-file.txt");
        String content = new String(FileCopyUtils.copyToByteArray(inputStream), StandardCharsets.UTF_8);

        // 处理 content
    }
}

方式5: 使用ResourceUtils

import org.springframework.util.ResourceUtils;
import java.io.File;

public class ResourceUtilsExample {
    public static void main(String[] args) throws IOException {
        File file = ResourceUtils.getFile("classpath:my-file.txt");

        // 进一步处理 file
    }
}

方式6: 使用FileInputStream

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class FileInputStreamExample {
    public static void main(String[] args) throws IOException {
        File file = new File(FileInputStreamExample.class.getClassLoader().getResource("my-file.txt").getFile());
        try (FileInputStream fis = new FileInputStream(file)) {
            // 读取文件内容
        }
    }
}

方式7: 使用SpringResourceLoader

import org.springframework.context.annotation.Bean;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import java.io.InputStream;

public class SpringResourceLoaderExample {
    @Bean
    public DefaultResourceLoader resourceLoader() {
        return new DefaultResourceLoader();
    }

    public void readFile() throws IOException {
        Resource resource = resourceLoader().getResource("classpath:my-file.txt");
        InputStream inputStream = resource.getInputStream();

        // 进一步处理 inputStream
    }
}

方式8: 使用ServletContext

import javax.servlet.ServletContext;
import java.io.InputStream;
import org.springframework.beans.factory.annotation.Autowired;

public class ServletContextExample {
    @Autowired
    private ServletContext servletContext;

    public void readFile() throws IOException {
        InputStream inputStream = servletContext.getResourceAsStream("/WEB-INF/classes/my-file.txt");

        // 进一步处理 inputStream
    }
}

方式9: 使用PathsFiles(适用于Spring Boot 2.x以上)

import org.springframework.core.io.Resource;
import org.springframework.core.io.DefaultResourceLoader;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Files;
import java.nio.charset.StandardCharsets;

public class PathsAndFilesExample {
    public static void main(String[] args) throws IOException {
        DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
        Resource resource = resourceLoader.getResource("classpath:my-file.txt");
        Path path = resource.getFile().toPath();
        String content = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);

        // 处理 content
    }
}

以上这9种方式可以让您轻松读取resources目录下的文件,选择适合您项目需求的方式,并根据示例代码来实现文件读取功能。希望这些方式能帮助您在Spring Boot项目中处理资源文件。


http://www.kler.cn/a/574550.html

相关文章:

  • 【DeepSeek】5分钟快速实现本地化部署教程
  • 【经验分享】Ubuntu20.04编译RK3568 AI模型报错问题(已解决)
  • Java TCP 通信:实现简单的 Echo 服务器与客户端
  • 单片机最小系统原理图设计
  • 【芯片设计】AI偏车载芯片前端设计工程师面试记录·20250304
  • Linux网络编程——TCP并行服务器
  • Swagger UI界面的使用
  • Ae 效果详解:VR 球面到平面
  • 【解决问题】conda 虚拟环境内,`pip list` 展示全局的包
  • 通往 AI 之路:Python 机器学习入门-机器学习基本概念
  • redis 过期键删除策略与回收策略
  • 2025中国AI大模型对比
  • Spring Boot API 项目中 HAProxy 与 Nginx 的选择与实践
  • 【Linux内核系列】:进入文件系统的世界
  • 激光雷达市场观察2-美国 PLI 发展脉络与核心技术解析2025.3.7
  • 安铂克科技 APPH 系列相位噪声分析仪:高性能测量的卓越之选
  • Sqlserver安全篇之_手工创建TLS用到的pfx证书文件
  • Halcon:HObject与opencv:Mat互转
  • 【目标检测】【CVPR 2025】DEIM:具有改进匹配机制的DETR以实现快速收敛
  • Scala:case class(通俗易懂版)