-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextFileWrite.java
More file actions
67 lines (50 loc) · 1.64 KB
/
TextFileWrite.java
File metadata and controls
67 lines (50 loc) · 1.64 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class TextFileWrite {
public static void main(String[] args) throws IOException {
UsePrintWriter("textwrite.txt");
UseFileWriter("textwrite1.txt");
}
static void writeListToFile(List<String> list, String outputFileName) throws IOException {
File file = new File(".", outputFileName);
FileWriter fw = new FileWriter(file);
for(String s: list) {
String data = s+"\r\n";
fw.write(data);
}
fw.close();
}
private static void UseFileWriter(String outputFileName) throws IOException {
File file = new File("./TEXT/", outputFileName);
FileWriter fw = new FileWriter(file);
for(int i=1; i<11; i++) {
String data = i+" 번째 줄입니다.\r\n";
fw.write(data);
}
fw.close();
FileWriter fw2 = new FileWriter(file, true);
for(int i=11; i<21; i++) {
String data = i+" 번째 줄입니다.\r\n";
fw2.write(data);
}
fw2.close();
}
private static void UsePrintWriter(String outputFileName) throws IOException {
File file = new File("./TEXT/", outputFileName);
PrintWriter pw = new PrintWriter(file);
for(int i=1; i<11; i++) {
String data = i+" 번째 줄입니다.";
pw.println(data);
}
pw.close();
PrintWriter pw2 = new PrintWriter(new FileWriter(file, true));
for(int i=11; i<21; i++) {
String data = i+" 번째 줄입니다2";
pw2.println(data);
}
pw2.close();
}
}