java 读取文本文件
1、新建springboot项目,如下
2、创建一个包含main方法的class
public class HellowWorldController {
public static void main(String[] args) {
String filePath = "D:\\test.txt";
FileReader fr = null;
BufferedReader bf = null;
try {
fr = new FileReader(filePath);
bf = new BufferedReader(fr);
String str = null;
// 按行读取字符串
while ((str = bf.readLine()) != null) {
System.out.println(str);
}
}catch (Exception e){
//log error
}finally {
try {
if(bf!=null){bf.close();}
if(fr!=null){fr.close();}
} catch (IOException e) {
//log error
}
}
}
}
3、在D盘新建了一个test.txt文件,内容为“hello world”
4、运行结果
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
阅读量:38
阅读量:66
阅读量:168
阅读量:188
阅读量:99