目錄
一、前提條件
2.1、Controller、service中使用ClassPathResource
一、前提條件
要去讀取的文件是存放在project/src/main/resources目錄下的,如下圖中的test.txt文件。
二、使用ClassPathResource類讀取
2.1、Controller、service中使用ClassPathResource
不管是在哪一層(service、controller..),都可以使用這種方式,甚至是單元測試中,也是可以的。
package cn.ganlixin.demo.controller; import org.springframework.core.io.ClassPathResource; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; @RestController public class TestController { @RequestMapping("testFile") public String testFile() throws IOException { // ClassPathResource類的構造方法接收路徑名稱,自動去classpath路徑下找文件 ClassPathResource classPathResource = new ClassPathResource("test.txt"); // 獲得File對象,當然也可以獲取輸入流對象 File file = classPathResource.getFile(); BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); StringBuilder content = new StringBuilder(); String line = null; while ((line = bufferedReader.readLine()) != null) { content.append(line); } return content.toString(); } }
2.2、單元測試使用ClassPathResource
單元測試也是可以使用ClassPathResource,但是需要注意:
1、單元測試的資源目錄默認是project/src/test/resources,如果該目錄下找到單元測試需要的文件,那么就用找到的文件;
2、如果在單元測試的資源目錄下沒有找到單元測試需要的文件,就會去找/project/src/main/resources目錄下的同名文件進行操作。
package cn.ganlixin.demo.example; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.core.io.ClassPathResource; import org.springframework.test.context.junit4.SpringRunner; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; @RunWith(SpringRunner.class) @SpringBootTest public class ApplicationConfigTest { @Test public void testFile() throws IOException { final ClassPathResource classPathResource = new ClassPathResource("test.txt"); final File file = classPathResource.getFile(); final BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); String line = null; while ((line = bufferedReader.readLine()) != null) { System.out.println(line); } } }
三、使用FileSystemResource類讀取文件
FileSystemResource這個類在找文件的時候就是按照給定的路徑名去找,默認的當前目錄就是項目根目錄。
使用該類來查找文件時,需要保證文件路徑完全正確,另外,在代碼中將路徑寫死是一個不好的習慣,特別是一個文件的路徑在不同的主機上的位置(層級目錄)不一定相同,所以我們開發過程中很少使用這種方式。
FileSystemResource的用法和ClassPathResource的用法相似,因為他們都繼承了AbstractResource這個抽象類。
package cn.ganlixin.demo.example; import org.junit.Test; import org.springframework.core.io.FileSystemResource; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; public class FileTest extends ApplicationConfigTest { @Test public void testFile() throws IOException { FileSystemResource resource = new FileSystemResource("./"); System.out.println(resource.getFile().getAbsolutePath()); // 傳入當前路徑,獲得的是項目根目錄:/Users/ganlixin/code/Spring/demo/example/. // 傳入根目錄路徑,獲得的就是操作系統的根目錄 resource = new FileSystemResource("/"); System.out.println(resource.getFile().getAbsolutePath()); // 輸出 / // 獲取單元測試resources目錄下的test.txt,需要指定詳細的路徑 resource = new FileSystemResource("src/test/resources/test.txt"); final File file = resource.getFile(); final BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); String line = null; while ((line = bufferedReader.readLine()) != null) { System.out.println(line); } } }
這里就列舉了兩種方式,還有其他很多方式,這兩種應該夠用了