1.正常情况springbot项目的resource目录下会反正项目使用到的很多文件所以这里编写一个读取demo目录如下图所示
在这里插入图片描述
2.复制代码直接运行

import org.springframework.core.io.ClassPathResource;
import java.nio.file.Files;
import java.util.stream.Stream;

/**
 * spring/springboot获取resource目录下的文件
 * spring/springboot读取resource目录下的文件
 */
public class TestGetResourceFile {
    public static void main(String[] args) {
        ClassPathResource resource = new ClassPathResource("test/demo.txt");
        System.out.println("文件名称"+resource.getFilename());
        try (Stream<String> stream = Files.lines(resource.getFile().toPath())){
            stream.forEach(System.out::println);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

3.运行结果
在这里插入图片描述

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐