1 使用FileWrite写文本文件
2
3
4
public
static
void useFileWriter(String fileName)
throws IOException {
5 File file =
new File(fileName);
6 FileWriter fileWriter =
new FileWriter(file);
7
8 fileWriter.write("it is a test");
9
10 fileWriter.close();
11 }
12
13
14
15 使用BufferedWrite写文本文件
16
17
18
public
static
void useBufferedWriter(String fileName)
throws IOException{
19 File file =
new File(fileName);
20 BufferedWriter bufferedWriter =
new BufferedWriter(
new FileWriter(fileName));
21
22 bufferedWriter.write("hello bufferedwrite");
23
24 bufferedWriter.flush();
25 bufferedWriter.close();
26 }
27
28
29
30 使用Files写文件,最简单
31
32
33
public
static
void useJdk8(String fileName)
throws IOException {
34 Files.write(Paths.get(fileName), "hello usejdk8".getBytes(), StandardOpenOption.CREATE);
35 }
36
37
38 私用FileOutputStream写文件
39
40
41
public
static
void useFileOutputStream(String fileName)
throws IOException{
42 File file =
new File(fileName);
43
44 FileOutputStream fileOutputStream =
new FileOutputStream(file);
45 fileOutputStream.write("hello fileoutputstream".getBytes());
46
47 fileOutputStream.flush();
48
49 fileOutputStream.close();
50 }
51
52
53
54 使用BufferedFileOutputStream写文件,速度最快,数据cache在jvm中,容易丢数据
55
56
57
public
static
void useBufferedFileOutputStream(String fileName) {
58 File file =
new File(fileName);
59
60
61 BufferedOutputStream bufferedOutputStream =
null;
62
try {
63 bufferedOutputStream =
new BufferedOutputStream(
new FileOutputStream(file));
64
65 bufferedOutputStream.write("hello BufferedFileOutputStream".getBytes());
66
67 bufferedOutputStream.flush();
68 }
catch(IOException e) {
69
70
71 }
finally {
72
if(bufferedOutputStream!=
null) {
73
try {
74 bufferedOutputStream.close();
75 }
catch (IOException e1) {
76
//
TODO do something
77
}
78 }
79 }
80
81 }
82
83
84
85 使用RandomAccessFile写文件,速度最慢,直接刷盘
86
87
88
public
static
void useRandomAccessFile(String fileName) {
89 RandomAccessFile randomAccessFile =
null;
90
91
92
try {
93 randomAccessFile =
new RandomAccessFile(fileName, "rw");
94 randomAccessFile.seek(15);
//
从第15个byte位置开始写, 原文件的第15个之后的字符会被覆盖一部分
95
randomAccessFile.write("useRandomAccessFile".getBytes());
96 }
catch (IOException e) {
97 e.printStackTrace();
98 }
finally {
99
if(randomAccessFile!=
null) {
100
try {
101 randomAccessFile.close();
102 }
catch (IOException e) {
103 e.printStackTrace();
104 }
105 }
106
107 }
108 }
109
110
111
112 使用FileChannel写文件
113
114
115
public
static
void useFileChannel(String fileName) {
116
117 FileChannel fileChannel =
null;
118
try {
119 FileChannel channel =
new FileOutputStream(fileName).getChannel();
120 channel.write(ByteBuffer.wrap("useFileChannel".getBytes()));
121 }
catch (IOException e) {
122 e.printStackTrace();
123 }
finally {
124
if(fileChannel!=
null) {
125
try {
126 fileChannel.close();
127 }
catch (IOException e) {
128 e.printStackTrace();
129 }
130 }
131 }
132 }
133
134
135
136 使用MappedByteBuffer写文件,速度快,OS级别内存映射
137
138
139
public
static
void useMappedByteBuffer(String fileName){
140
141 RandomAccessFile randomAccessFile =
null;
142
143
try {
144 randomAccessFile =
new RandomAccessFile(fileName, "rw");
145 FileChannel fileChannel = randomAccessFile.getChannel();
146
147
148 String content = "useMappedByteBuffer";
149
150 MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, content.getBytes().length);
151
152 mappedByteBuffer.put(content.getBytes());
//
mappedByteBuffer大小不能小于content的字节数
153
154
155 }
catch (IOException e) {
156 e.printStackTrace();
157 }
finally {
158
if(randomAccessFile!=
null) {
159
try {
160 randomAccessFile.close();
161 }
catch (IOException e) {
162 e.printStackTrace();
163 }
164 }
165 }
166 }
转载于:https://www.cnblogs.com/jym-sunshine/p/5613484.html