springboot如何读取resources下的文件

项目中很多时候需要读取自定义配置文件,本地开发工具怎么写都成功但是部署到服务其上就出现问题,

异常BOOT-INF/classes!/config.xml (文件名、目录名或卷标语法不正确.)路径中带有叹号之类的

了解了大概之后就是springboot打成jar是一个文件,也就是一个压缩包,没有办法读取压缩文件里的路径,因此要解决这个问题了解读取配置文件的原理,直接获取文件流就可以了。

springboot如何读取resources下的文件

1、使用项目内路径读取,只能在开发工具中使用,部署之后无法读取。(不通用

类似:src/main/resources/default.xml

File file = new File(“src/main/resources/default.xml”);

    @Test    public void testReadFile2() throws IOException {        File file = new File("src/main/resources/default.xml");        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();    }

登录后复制

 2、使用org.springframework.util.ResourceUtils,读取。在linux环境中无法读取。(不通用)

File file = ResourceUtils.getFile(“classpath:default.xml”);
FileInputStream fis = new FileInputStream(file);

    @Test    public void testReadFile3() throws IOException {        File file = ResourceUtils.getFile("classpath:default.xml");        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();    }

登录后复制

3、使用org.springframework.core.io.ClassPathResource,各种环境都能读取。(通用)

Resource resource = new ClassPathResource(“resource.properties”);
InputStream is = resource.getInputStream();

    @Test    public void testReadFile() throws IOException {//        ClassPathResource classPathResource = new ClassPathResource("default.xml");        Resource resource = new ClassPathResource("default.xml");        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();    }

登录后复制

4、结合spring注解,使用org.springframework.core.io.ResourceLoader;类的注解。(通用)

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader; import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.core.io.Resource;import org.springframework.core.io.ResourceLoader;import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class)@SpringBootTestpublic class ApplicationTests {     @Autowired    ResourceLoader resourceLoader;            @Test    public void testReaderFile() throws IOException {        Resource resource = resourceLoader.getResource("classpath:default.xml");        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();    }}

登录后复制

以上就是springboot如何读取resources下的文件的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2626993.html

(0)
上一篇 2025年3月7日 00:59:21
下一篇 2025年2月23日 21:46:01

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论