-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileOutputShortcut.java
More file actions
36 lines (28 loc) · 1.06 KB
/
Copy pathFileOutputShortcut.java
File metadata and controls
36 lines (28 loc) · 1.06 KB
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
package Chapter18_IO;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
/**
* 文本文件输出的快捷方式 (PrintWriter)
*/
public class FileOutputShortcut {
static String file = "src\\Chapter18_IO\\FileOutputShortcut.out";
public static void main(String[] args) throws IOException {
// 定义 Reader
BufferedReader in = new BufferedReader(
new StringReader(
BufferedInputFile.read("src\\Chapter18_IO\\FileOutputShortcut.java")));
// 定义 Writer
// >> SE5之后 PrintWriter 可直接传入 file, 无需装饰.
// >> 即: new PrintWriter(file) 代替 new PrintWriter(new BufferedWriter(new FileWriter(file)))
PrintWriter out = new PrintWriter(file);
int lineCount = 1;
String s;
while ((s = in.readLine()) != null)
out.println(lineCount++ + ": " + s);
out.close();
// Show the stored file:
System.out.println(BufferedInputFile.read(file));
}
}