您的位置:首页 > 编程语言 > Java开发

java读取文本文件内容

2016-03-30 10:32 507 查看
示例代码如下:

public static void main(String[] args) {
String path = "D:/demo.txt";
String line = null;
String content = ""; // 文件内容
BufferedReader br = null;
try {
// br = new BufferedReader(new FileReader(path));
br = new BufferedReader(new FileReader(new File(path))); // 与上等效

while (null != (line = br.readLine())) {
System.out.println(line);
content += line + "\n";
}
br.close();
System.out.println("--------------风骚分隔线--------------");
System.out.println(content);
} catch (Exception e) {
e.printStackTrace();
}
}


打印执行结果:

hello, world!!!

hello, world!!!
--------------风骚分隔线--------------
hello, world!!!

hello, world!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: