SpringBoot获取resources目录下的文件
在 Spring Boot 项目中,获取 resources 目录中的文件路径通常涉及到访问类路径资源(classpath resources)。Spring Boot 提供了一些工具类和方法,可以方便地访问这些资源。以下是一些常见的方法:
首先,我们在 Spring Boot 项目中的 resources (资源文件目录)下创建 file 目录,然后在 file 目录下创建 myBlog.txt 文件,并在文件中输入内容:您好,欢迎访问 pan_junbiao的博客。

1、使用项目路径
使用字符串方式写入文件的项目路径,这是最简单的方式。
String path = "src/main/resources/file/myBlog.txt";
@Test
public void readFileByPath() throws IOException
{
String path = "src/main/resources/file/myBlog.txt";
File file = new File(path);
if (file.exists())
{
try (BufferedReader reader = new BufferedReader(new FileReader(file)))
{
String line;
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
}
} else
{
System.out.println("未找到文件!");
}
}执行结果:

2、使用 ApplicationContext 接口
ApplicationContext 是 Spring 框架中的一个核心接口,它是 Spring IoC 容器的实现之一,用于管理和组织应用程序中的各种 Bean,同时提供了一系列功能来支持依赖注入、AOP 等特性。同时 ApplicationContext 提供了对资源的访问能力,如文件、URL等。这通过 Resource 接口和 ResourceLoader 接口实现,使得访问外部资源变得简单。
@Autowired
private ApplicationContext applicationContext;
@Test
public void readResourceFile() throws IOException
{
Resource resource = applicationContext.getResource("classpath:/file/myBlog.txt");
InputStream inputStream = resource.getInputStream();
if (inputStream != null) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
} else {
System.out.println("File not found");
}
}3、使用 ResourceLoader 接口
ResourceLoader 是 Spring 提供的一个接口,用于加载资源。你可以在 Spring Bean 中注入 ResourceLoader,然后使用它来加载资源。
@Autowired
private ResourceLoader resourceLoader;
@Test
public void readFileByResourceLoader() throws IOException
{
Resource resource = resourceLoader.getResource("classpath:/file/myBlog.txt");
if (resource.exists())
{
Path path = Paths.get(resource.getURI());
String content = new String(Files.readAllBytes(path));
System.out.println(content);
} else
{
System.out.println("未找到文件!");
}
}4、使用 ClassLoader 类
你也可以使用当前类的 ClassLoader 来加载资源。ClassLoader 的 getResource 和 getResourceAsStream 方法可以访问类路径资源。
@Test
public void readFileByClassLoader() throws IOException
{
ClassLoader classLoader = getClass().getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("file/myBlog.txt");
if (inputStream != null)
{
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)))
{
String line;
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
}
} else
{
System.out.println("未找到文件!");
}
}5、使用 PathMatchingResourcePatternResolver 类
PathMatchingResourcePatternResolver 是 Spring 提供的一个工具类,用于解析资源路径模式。它扩展了 ResourceLoader 的功能。
@Test
public void readFileByResourcePattern() throws IOException
{
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource resource = resolver.getResource("classpath:/file/myBlog.txt");
if (resource.exists())
{
Path path = resource.getFile().toPath();
String content = new String(Files.readAllBytes(path));
System.out.println(content);
} else
{
System.out.println("未找到文件!");
}
}注意:resource.getFile() 方法在某些情况下可能会抛出 UnsupportedOperationException,特别是在资源是从 JAR 文件中加载时。所以,更通用的方法是使用 InputStream 来读取文件内容。
到此这篇关于SpringBoot获取resources目录下的文件的文章就介绍到这了,更多相关SpringBoot获取resources目录下文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
spring boot使用@Async注解解决异步多线程入库的问题
最近在写项目是需要添加异步操作来提高效率,所以下面这篇文章主要给大家介绍了关于spring boot使用@Async注解解决异步多线程入库问题的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下2022-05-05
Zuul1与Spring Cloud Gateway的区别及说明
Zuul1基于Servlet阻塞IO,稳定但高并发易耗尽线程;SpringCloudGateway采用非阻塞IO和Netty,性能更优、内置限流,适合高并发场景,两者均支持SpringCloud集成,但Zuul1有更多生产落地案例2025-07-07
SpringBoot自动装配之多数据源SDK解决Dubbo性能瓶颈详解
这篇文章主要为大家详细介绍了SpringBoot自动装配中如何通过多数据源SDK解决Dubbo性能瓶颈,文中的示例代码讲解详细,有需要的小伙伴可以了解下2025-09-09


最新评论