-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathWriteTextFile.java
51 lines (48 loc) · 1.3 KB
/
WriteTextFile.java
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
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
/**
* @author Edwin Torres
* email: [email protected]
*
* description:
* This program shows how to write output to
* a text file. If the file exists, it will
* overwrite it.
*
*/
public class WriteTextFile {
public static void main(String[] args) {
String fullPathOutFile = "C:\\Temp\\out.txt";
BufferedWriter bw = null;
FileWriter fw = null;
File outputFile = new File(fullPathOutFile);
try {
fw = new FileWriter(outputFile.getAbsoluteFile());
bw = new BufferedWriter(fw);
bw.write("A");
bw.newLine();
bw.write("B");
bw.newLine();
bw.write("C");
bw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fw != null)
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}