Spring Boot项目 获取静态资源文件

直接上代码

public static void main(String[] args) {
    String str = null;
    try {
        Resource resource = new ClassPathResource("static/abc.txt");
        BufferedReader bReader = null;
        bReader = new BufferedReader(new InputStreamReader(resource.getInputStream()));
        StringBuffer sb = new StringBuffer();
        String s = "";
        while ((s = bReader.readLine()) != null) {//逐行读取文件内容,不读取换行符和末尾的空格
            sb.append(s + "\n");//将读取的字符串添加换行符后累加存放在缓存中
        }
        str = sb.toString();
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println(str);
}

"static/abc.txt" 全路径是 D:\gitworkspace\project\src\main\resources\static\abc.txt

你可能感兴趣的:(web,开发常用,java,springboot,静态文件)