读写文件文件

This commit is contained in:
2024-12-11 14:09:38 +08:00
parent fdaf7334c8
commit 4ba14576e7
5 changed files with 120 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
package chapter7;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Example10 {
public static void main(String[] args) {
// 使用try-with-resources自动关闭资源
try (BufferedReader reader = new BufferedReader(
new FileReader("note.txt"))) {
String line;
// 逐行读取文件内容
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.out.println("读取文件出错:" + e.getMessage());
}
}
}