读写文件文件
This commit is contained in:
23
src/chapter7/Example10.java
Normal file
23
src/chapter7/Example10.java
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user