示例代码如下:
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!!!