1. 读取txt文件:
使用ClassPathResource的原因在https://blog.csdn.net/nnnora/article/details/80734326中阐述过,因为是springboot内置web容器所以不能使用FileInputStream的方式获取文件流。
//读取txt文件
ClassPathResource classPathResource = new ClassPathResource("test.txt");
InputStreamReader inputStreamReader = new InputStreamReader(classPathResource.getInputStream());
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuffer buffer = new StringBuffer();
String line = null;
while( (line = bufferedReader.readLine()) != null ){
buffer.append(line);
}
bufferedReader.close();
String buffer = buffer.toString();
2. 写入txt文件:
//写入txt
BufferedWriter bufw &#