1、前提條件java
2.一、Controller、service中使用ClassPathResourcespring
2.二、單元測試使用ClassPathResourceapp
3、使用FileSystemResource類讀取文件單元測試
要去讀取的文件是存放在project/src/main/resources目錄下的,以下圖中的test.txt文件。測試
無論是在哪一層(service、controller..),均可以使用這種方式,甚至是單元測試中,也是能夠的。ui
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(); } }
單元測試也是可使用ClassPathResource,可是須要注意:spa
一、單元測試的資源目錄默認是project/src/test/resources,若是該目錄下找到單元測試須要的文件,那麼就用找到的文件;操作系統
二、若是在單元測試的資源目錄下沒有找到單元測試須要的文件,就會去找/project/src/main/resources目錄下的同名文件進行操做。code
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的用法和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); } } }
這裏就列舉了兩種方式,還有其餘不少方式,這兩種應該夠用了