SpringBoot獲取resources目錄下的文件
在 Spring Boot 項目中,獲取 resources 目錄中的文件路徑通常涉及到訪問類路徑資源(classpath resources)。Spring Boot 提供了一些工具類和方法,可以方便地訪問這些資源。以下是一些常見的方法:
首先,我們在 Spring Boot 項目中的 resources (資源文件目錄)下創(chuàng)建 file 目錄,然后在 file 目錄下創(chuàng)建 myBlog.txt 文件,并在文件中輸入內(nèi)容:您好,歡迎訪問 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("未找到文件!"); } }
執(zhí)行結(jié)果:
2、使用 ApplicationContext 接口
ApplicationContext 是 Spring 框架中的一個核心接口,它是 Spring IoC 容器的實現(xiàn)之一,用于管理和組織應用程序中的各種 Bean,同時提供了一系列功能來支持依賴注入、AOP 等特性。同時 ApplicationContext 提供了對資源的訪問能力,如文件、URL等。這通過 Resource 接口和 ResourceLoader 接口實現(xià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 提供的一個接口,用于加載資源。你可以在 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 來讀取文件內(nèi)容。
到此這篇關于SpringBoot獲取resources目錄下的文件的文章就介紹到這了,更多相關SpringBoot獲取resources目錄下文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring Boot使用RestTemplate消費REST服務的幾個問題記錄
這篇文章主要介紹了Spring Boot使用RestTemplate消費REST服務的幾個問題記錄,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06零基礎學Java:Java開發(fā)工具 Eclipse 安裝過程創(chuàng)建第一個Java項目及Eclipse的一些基礎使用技巧
這篇文章主要介紹了零基礎學Java:Java開發(fā)工具 Eclipse 安裝過程創(chuàng)建第一個Java項目及Eclipse的一些基礎使用技巧,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09Spring中的@Value和@PropertySource注解詳解
這篇文章主要介紹了Spring中的@Value和@PropertySource注解詳解,@PropertySource:讀取外部配置文件中的key-value保存到運行的環(huán)境變量中,本文提供了部分實現(xiàn)代碼,需要的朋友可以參考下2023-11-11基于Rest的API解決方案(jersey與swagger集成)
下面小編就為大家?guī)硪黄赗est的API解決方案(jersey與swagger集成)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08淺談java 增強型的for循環(huán) for each
下面小編就為大家?guī)硪黄獪\談java 增強型的for循環(huán) for each。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10