SpringBoot读取Resources下文件

2022-08-28 10:49:35

问题:

需要读取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包下的文件

  • 作者:hwt1070359898
  • 原文链接:https://blog.csdn.net/hwt1070359898/article/details/120726071
    更新时间:2022-08-28 10:49:35