今天用到spring的工具類去獲取classpath下的資源文件,在此記錄下
1、資源文件不在jar中
import java.io.File;
import org.springframework.util.ResourceUtils;
File testFile = ResourceUtils.getFile("classpath:test.txt");
或者
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
Resource fileRource = new ClassPathResource("test.txt");
獲取文件:fileRource.getFile();
獲取文件流:fileRource.getInputStream();
2、資源文件在jar中
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
Resource fileRource = new ClassPathResource("test.txt");
獲取文件流:fileRource.getInputStream();