-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTxtFileWrite.java
More file actions
50 lines (45 loc) · 1.43 KB
/
TxtFileWrite.java
File metadata and controls
50 lines (45 loc) · 1.43 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
package java_Files;
import java.io.*;
public class TxtFileWrite {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 尽量采用try-source 写法
writeFile1();
System.out.println("===================");
// writeFile2();JDk 7以上可以使用
}
public static void writeFile1() {
FileOutputStream fos = null;// 节点类,负责写字节
OutputStreamWriter osw = null;// 转化类,负责字符到字节的转化
BufferedWriter bw = null;// 装饰类,负责写字符到缓存区
// 三者的关系BufferedWriter(OutputStreamWriter(FileOutputStream))
try {
fos = new FileOutputStream("c:/temp/abc.txt"); // 节点类
osw = new OutputStreamWriter(fos, "UTF-8"); // 转化类
bw = new BufferedWriter(osw); // 装饰类
bw.write("我们是");
bw.newLine();
bw.write("Ecnuers.^^");
bw.newLine();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
bw.close(); // 关闭最后一个类,会将所有底层流都关闭
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public static void writeFile2() {
// try-resource 自动关闭资源 java1.7以上可以使用
try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("c:/temp/abc.txt")))) {
bw.write("������");
bw.newLine();
bw.write("Ecnuers.^^");
bw.newLine();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}