-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicFileOutput.java
More file actions
38 lines (29 loc) · 1002 Bytes
/
Copy pathBasicFileOutput.java
File metadata and controls
38 lines (29 loc) · 1002 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package Chapter18_IO;
import java.io.*;
/**
* 基本文件的输出 ( Reader -> Writer)
*/
public class BasicFileOutput {
static String file = "src\\Chapter18_IO\\BasicFileOut.out";
public static void main(String[] args) throws IOException {
// 定义 Reader
BufferedReader in = new BufferedReader(
new StringReader(
BufferedInputFile.read("src\\Chapter18_IO\\BasicFileOutput.java")
)
);
// 定义 Writer :FileWriter 装饰成 PrintWriter
PrintWriter out = new PrintWriter(
new BufferedWriter(
new FileWriter(file)
)
);
int lineCount = 1; // 打印行号用
String s;
while ((s = in.readLine()) != null)
out.println(lineCount++ + ": " + s);
// 如果不关闭, 缓冲区不会刷新清空
out.close();
System.out.println(BufferedInputFile.read(file));
}
}