直接上代碼
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