-
Notifications
You must be signed in to change notification settings - Fork 8
/
writing-files.html
286 lines (220 loc) · 11.9 KB
/
writing-files.html
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go by Example 中文版: 写文件</title>
<link rel=stylesheet href="site.css">
</head>
<script>
onkeydown = (e) => {
if (e.key == "ArrowLeft") {
window.location.href = 'reading-files';
}
if (e.key == "ArrowRight") {
window.location.href = 'line-filters';
}
}
</script>
<body>
<div class="example" id="writing-files">
<h2><a href="./">Go by Example 中文版</a>: 写文件</h2>
<table>
<tr>
<td class="docs">
<p>在 Go 中,写文件与我们前面看过的读文件方法类似。</p>
</td>
<td class="code empty leading">
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<a href="https://play.studygolang.com/p/2CEMRivZzqA"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
<pre class="chroma"><span class="kn">package</span> <span class="nx">main</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kn">import</span> <span class="p">(</span>
<span class="s">"bufio"</span>
<span class="s">"fmt"</span>
<span class="s">"os"</span>
<span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kd">func</span> <span class="nf">check</span><span class="p">(</span><span class="nx">e</span> <span class="kt">error</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="nx">e</span> <span class="o">!=</span> <span class="kc">nil</span> <span class="p">{</span>
<span class="nb">panic</span><span class="p">(</span><span class="nx">e</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kd">func</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>开始!这里展示了如何写入一个字符串(或者只是一些字节)到一个文件。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">d1</span> <span class="o">:=</span> <span class="p">[]</span><span class="nb">byte</span><span class="p">(</span><span class="s">"hello\ngo\n"</span><span class="p">)</span>
<span class="nx">err</span> <span class="o">:=</span> <span class="nx">os</span><span class="p">.</span><span class="nf">WriteFile</span><span class="p">(</span><span class="s">"/tmp/dat1"</span><span class="p">,</span> <span class="nx">d1</span><span class="p">,</span> <span class="mo">0644</span><span class="p">)</span>
<span class="nf">check</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>对于更细粒度的写入,先打开一个文件。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">f</span><span class="p">,</span> <span class="nx">err</span> <span class="o">:=</span> <span class="nx">os</span><span class="p">.</span><span class="nf">Create</span><span class="p">(</span><span class="s">"/tmp/dat2"</span><span class="p">)</span>
<span class="nf">check</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>打开文件后,一个习惯性的操作是:立即使用 defer 调用文件的 <code>Close</code>。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="k">defer</span> <span class="nx">f</span><span class="p">.</span><span class="nf">Close</span><span class="p">()</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>您可以按期望的那样 <code>Write</code> 字节切片。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">d2</span> <span class="o">:=</span> <span class="p">[]</span><span class="kt">byte</span><span class="p">{</span><span class="mi">115</span><span class="p">,</span> <span class="mi">111</span><span class="p">,</span> <span class="mi">109</span><span class="p">,</span> <span class="mi">101</span><span class="p">,</span> <span class="mi">10</span><span class="p">}</span>
<span class="nx">n2</span><span class="p">,</span> <span class="nx">err</span> <span class="o">:=</span> <span class="nx">f</span><span class="p">.</span><span class="nf">Write</span><span class="p">(</span><span class="nx">d2</span><span class="p">)</span>
<span class="nf">check</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Printf</span><span class="p">(</span><span class="s">"wrote %d bytes\n"</span><span class="p">,</span> <span class="nx">n2</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p><code>WriteString</code> 也是可用的。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">n3</span><span class="p">,</span> <span class="nx">err</span> <span class="o">:=</span> <span class="nx">f</span><span class="p">.</span><span class="nf">WriteString</span><span class="p">(</span><span class="s">"writes\n"</span><span class="p">)</span>
<span class="nf">check</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Printf</span><span class="p">(</span><span class="s">"wrote %d bytes\n"</span><span class="p">,</span> <span class="nx">n3</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>调用 <code>Sync</code> 将缓冲区的数据写入硬盘。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">f</span><span class="p">.</span><span class="nf">Sync</span><span class="p">()</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>与我们前面看到的带缓冲的 Reader 一样,<code>bufio</code> 还提供了的带缓冲的 Writer。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">w</span> <span class="o">:=</span> <span class="nx">bufio</span><span class="p">.</span><span class="nf">NewWriter</span><span class="p">(</span><span class="nx">f</span><span class="p">)</span>
<span class="nx">n4</span><span class="p">,</span> <span class="nx">err</span> <span class="o">:=</span> <span class="nx">w</span><span class="p">.</span><span class="nf">WriteString</span><span class="p">(</span><span class="s">"buffered\n"</span><span class="p">)</span>
<span class="nf">check</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Printf</span><span class="p">(</span><span class="s">"wrote %d bytes\n"</span><span class="p">,</span> <span class="nx">n4</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>使用 <code>Flush</code> 来确保,已将所有的缓冲操作应用于底层 writer。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">w</span><span class="p">.</span><span class="nf">Flush</span><span class="p">()</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code">
<pre class="chroma"><span class="p">}</span>
</pre>
</td>
</tr>
</table>
<table>
<tr>
<td class="docs">
<p>运行这段文件写入代码。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="gp">$</span> go run writing-files.go
<span class="go">wrote 5 bytes
</span><span class="go">wrote 7 bytes
</span><span class="go">wrote 9 bytes</span></pre>
</td>
</tr>
<tr>
<td class="docs">
<p>然后检查写入文件的内容。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="gp">$</span> cat /tmp/dat1
<span class="go">hello
</span><span class="go">go
</span><span class="go"></span><span class="gp">$</span> cat /tmp/dat2
<span class="go">some
</span><span class="go">writes
</span><span class="go">buffered</span></pre>
</td>
</tr>
<tr>
<td class="docs">
<p>我们刚刚看到了文件 I/O 思想,
接下来,我们看看它在 <code>stdin</code> 和 <code>stdout</code> 流中的应用。</p>
</td>
<td class="code empty">
</td>
</tr>
</table>
<p class="next">
下一个例子: <a href="line-filters">行过滤器</a>
</p>
<p class="footer">
<a href="https://twitter.com/mmcgrana">@mmcgrana</a> 和<a href="https://eli.thegreenplace.net">Eli Bendersky</a>编写 | <a href="https://github.com/gobyexample-cn">gobyexample-cn</a> 翻译 | <a href="https://github.com/gobyexample-cn/gobyexample/issues">反馈</a> | <a href="https://github.com/gobyexample-cn/gobyexample">源码</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a> </p>
</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"bufio\"\u000A \"fmt\"\u000A \"os\"\u000A)\u000A');codeLines.push('func check(e error) {\u000A if e !\u003D nil {\u000A panic(e)\u000A }\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' d1 :\u003D []byte(\"hello\\ngo\\n\")\u000A err :\u003D os.WriteFile(\"/tmp/dat1\", d1, 0644)\u000A check(err)\u000A');codeLines.push(' f, err :\u003D os.Create(\"/tmp/dat2\")\u000A check(err)\u000A');codeLines.push(' defer f.Close()\u000A');codeLines.push(' d2 :\u003D []byte{115, 111, 109, 101, 10}\u000A n2, err :\u003D f.Write(d2)\u000A check(err)\u000A fmt.Printf(\"wrote %d bytes\\n\", n2)\u000A');codeLines.push(' n3, err :\u003D f.WriteString(\"writes\\n\")\u000A check(err)\u000A fmt.Printf(\"wrote %d bytes\\n\", n3)\u000A');codeLines.push(' f.Sync()\u000A');codeLines.push(' w :\u003D bufio.NewWriter(f)\u000A n4, err :\u003D w.WriteString(\"buffered\\n\")\u000A check(err)\u000A fmt.Printf(\"wrote %d bytes\\n\", n4)\u000A');codeLines.push(' w.Flush()\u000A');codeLines.push('}\u000A');codeLines.push('');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>
</html>