SpringBoot獲取resources目錄下的文件
在 Spring Boot 項(xiàng)目中,獲取 resources 目錄中的文件路徑通常涉及到訪問(wèn)類路徑資源(classpath resources)。Spring Boot 提供了一些工具類和方法,可以方便地訪問(wèn)這些資源。以下是一些常見(jiàn)的方法:
首先,我們?cè)?Spring Boot 項(xiàng)目中的 resources (資源文件目錄)下創(chuàng)建 file 目錄,然后在 file 目錄下創(chuàng)建 myBlog.txt 文件,并在文件中輸入內(nèi)容:您好,歡迎訪問(wèn) pan_junbiao的博客。

1、使用項(xiàng)目路徑
使用字符串方式寫入文件的項(xiàng)目路徑,這是最簡(jiǎn)單的方式。
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("未找到文件!");
}
}執(zhí)行結(jié)果:

2、使用 ApplicationContext 接口
ApplicationContext 是 Spring 框架中的一個(gè)核心接口,它是 Spring IoC 容器的實(shí)現(xiàn)之一,用于管理和組織應(yīng)用程序中的各種 Bean,同時(shí)提供了一系列功能來(lái)支持依賴注入、AOP 等特性。同時(shí) ApplicationContext 提供了對(duì)資源的訪問(wèn)能力,如文件、URL等。這通過(guò) Resource 接口和 ResourceLoader 接口實(shí)現(xiàn),使得訪問(wèn)外部資源變得簡(jiǎn)單。
@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 提供的一個(gè)接口,用于加載資源。你可以在 Spring Bean 中注入 ResourceLoader,然后使用它來(lái)加載資源。
@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 類
你也可以使用當(dāng)前類的 ClassLoader 來(lái)加載資源。ClassLoader 的 getResource 和 getResourceAsStream 方法可以訪問(wèn)類路徑資源。
@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 提供的一個(gè)工具類,用于解析資源路徑模式。它擴(kuò)展了 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() 方法在某些情況下可能會(huì)拋出 UnsupportedOperationException,特別是在資源是從 JAR 文件中加載時(shí)。所以,更通用的方法是使用 InputStream 來(lái)讀取文件內(nèi)容。
到此這篇關(guān)于SpringBoot獲取resources目錄下的文件的文章就介紹到這了,更多相關(guān)SpringBoot獲取resources目錄下文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot使用RestTemplate消費(fèi)REST服務(wù)的幾個(gè)問(wèn)題記錄
這篇文章主要介紹了Spring Boot使用RestTemplate消費(fèi)REST服務(wù)的幾個(gè)問(wèn)題記錄,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
Spring中的@Value和@PropertySource注解詳解
這篇文章主要介紹了Spring中的@Value和@PropertySource注解詳解,@PropertySource:讀取外部配置文件中的key-value保存到運(yùn)行的環(huán)境變量中,本文提供了部分實(shí)現(xiàn)代碼,需要的朋友可以參考下2023-11-11
基于Rest的API解決方案(jersey與swagger集成)
下面小編就為大家?guī)?lái)一篇基于Rest的API解決方案(jersey與swagger集成)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
淺談java 增強(qiáng)型的for循環(huán) for each
下面小編就為大家?guī)?lái)一篇淺談java 增強(qiáng)型的for循環(huán) for each。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10

