栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot读取Resources下文件

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot读取Resources下文件

问题:

需要读取resources下的文件,文件格式不定,这里以txt为例,主要说明路径问题:

一、使用项目内路径读取,该路径只在开发工具中显示,类似:src/main/resources/resource.properties。只能在开发工具中使用,部署之后无法读取。(不通用) 主要代码:
    File file = new File("src/main/resources/downloadApp.txt");
    @Test
    public void testReadFile2() throws IOException {
        File file = new File("src/main/resources/downloadApp.txt");
        FileInputStream fis = new FileInputStream(file);
        InputStreamReader isr = new InputStreamReader(fis);
        BufferedReader br = new BufferedReader(isr);
        String data = null;
        while((data = br.readLine()) != null) {
            System.out.println(data);
        }
        
        br.close();
        isr.close();
        fis.close();
    }
二、使用org.springframework.util.ResourceUtils,读取。在linux环境中无法读取。(不通用) 主要代码:
    File file = ResourceUtils.getFile("classpath:downloadApp.txt");
    FileInputStream fis = new FileInputStream(file);
    @Test
    public void testReadFile3() throws IOException {
        File file = ResourceUtils.getFile("classpath:downloadApp.txt");
        FileInputStream fis = new FileInputStream(file);
        InputStreamReader isr = new InputStreamReader(fis);
        BufferedReader br = new BufferedReader(isr);
        String data = null;
        while((data = br.readLine()) != null) {
            System.out.println(data);
        }
        
        br.close();
        isr.close();
        fis.close();
    }
三、使用org.springframework.core.io.ClassPathResource,各种环境都能读取。(通用) 主要代码:
    Resource resource = new ClassPathResource("downloadApp.txt");
    InputStream is = resource.getInputStream();
    @Test
    public void testReadFile() throws IOException {

        Resource resource = new ClassPathResource("downloadApp.txt");
        InputStream is = resource.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String data = null;
        while((data = br.readLine()) != null) {
            System.out.println(data);
        }
        
        br.close();
        isr.close();
        is.close();
    }
四、结合spring注解,使用org.springframework.core.io.ResourceLoader;类注解。(通用)
@RunWith(SpringRunner.class)
@SpringBootTest
public class EttpCustomApplicationTests {

    @Autowired
    ResourceLoader resourceLoader;
    
    
    @Test
    public void testReaderFile() throws IOException {
        Resource resource = resourceLoader.getResource("classpath:downloadApp.txt");
        InputStream is = resource.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String data = null;
        while((data = br.readLine()) != null) {
            System.out.println(data);
        }
        
        br.close();
        isr.close();
        is.close();
    }

}
PS:

使用了以上方式仍然读取不到resources包下的文件时,建议将打好的jar包反编译下,看下代码是否成功将资源文件打入到jar包内,如果没有打入到jar包,则不是读取方式的问题,需要找找为何未打入jar包的原因,这里贴一个笔者遇到的一个坑,希望能帮到大家~~

以前的大佬在pom文件中配置了打包时允许打入jar包的资源包下的文件类型,笔者开始就是因为没有放开txt类型文件,所以未打入到jar包中,导致读取失败的,本地也会出现,因为本地编译时,读取的也是字节码文件里的文件,即target包里面的文件,而不是java包下的文件

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/318758.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号