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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章