-
Notifications
You must be signed in to change notification settings - Fork 8
/
reading-files.html
278 lines (218 loc) · 14.8 KB
/
reading-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
<!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 = 'base64-encoding';
}
if (e.key == "ArrowRight") {
window.location.href = 'writing-files';
}
}
</script>
<body>
<div class="example" id="reading-files">
<h2><a href="./">Go by Example 中文版</a>: 读文件</h2>
<table>
<tr>
<td class="docs">
<p>读写文件在很多程序中都是必须的基本任务。
首先我们来看一些读文件的例子。</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/zBe9iUpeaXJ"><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">"io"</span>
<span class="s">"os"</span>
<span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>读取文件需要经常进行错误检查,
这个帮助方法可以精简下面的错误检查过程。</p>
</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">dat</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">ReadFile</span><span class="p">(</span><span class="s">"/tmp/dat"</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">Print</span><span class="p">(</span><span class="nb">string</span><span class="p">(</span><span class="nx">dat</span><span class="p">))</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>您通常会希望对文件的读取方式和内容进行更多控制。
对于这个任务,首先使用 <code>Open</code> 打开一个文件,以获取一个 <code>os.File</code> 值。</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">Open</span><span class="p">(</span><span class="s">"/tmp/dat"</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>从文件的开始位置读取一些字节。
最多允许读取 5 个字节,但还要注意实际读取了多少个。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">b1</span> <span class="o">:=</span> <span class="nb">make</span><span class="p">([]</span><span class="kt">byte</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
<span class="nx">n1</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">Read</span><span class="p">(</span><span class="nx">b1</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">"%d bytes: %s\n"</span><span class="p">,</span> <span class="nx">n1</span><span class="p">,</span> <span class="nb">string</span><span class="p">(</span><span class="nx">b1</span><span class="p">[:</span><span class="nx">n1</span><span class="p">]))</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>你也可以 <code>Seek</code> 到一个文件中已知的位置,并从这个位置开始读取。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">o2</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">Seek</span><span class="p">(</span><span class="mi">6</span><span class="p">,</span> <span class="mi">0</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">b2</span> <span class="o">:=</span> <span class="nb">make</span><span class="p">([]</span><span class="kt">byte</span><span class="p">,</span> <span class="mi">2</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">Read</span><span class="p">(</span><span class="nx">b2</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">"%d bytes @ %d: "</span><span class="p">,</span> <span class="nx">n2</span><span class="p">,</span> <span class="nx">o2</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">"%v\n"</span><span class="p">,</span> <span class="nb">string</span><span class="p">(</span><span class="nx">b2</span><span class="p">[:</span><span class="nx">n2</span><span class="p">]))</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>例如,<code>io</code> 包提供了一个更健壮的实现 <code>ReadAtLeast</code>,用于读取上面那种文件。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">o3</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">Seek</span><span class="p">(</span><span class="mi">6</span><span class="p">,</span> <span class="mi">0</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">b3</span> <span class="o">:=</span> <span class="nb">make</span><span class="p">([]</span><span class="kt">byte</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="nx">n3</span><span class="p">,</span> <span class="nx">err</span> <span class="o">:=</span> <span class="nx">io</span><span class="p">.</span><span class="nf">ReadAtLeast</span><span class="p">(</span><span class="nx">f</span><span class="p">,</span> <span class="nx">b3</span><span class="p">,</span> <span class="mi">2</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">"%d bytes @ %d: %s\n"</span><span class="p">,</span> <span class="nx">n3</span><span class="p">,</span> <span class="nx">o3</span><span class="p">,</span> <span class="nb">string</span><span class="p">(</span><span class="nx">b3</span><span class="p">))</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>没有内建的倒带,但是 <code>Seek(0, 0)</code> 实现了这一功能。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">_</span><span class="p">,</span> <span class="nx">err</span> <span class="p">=</span> <span class="nx">f</span><span class="p">.</span><span class="nf">Seek</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</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><code>bufio</code> 包实现了一个缓冲读取器,这可能有助于提高许多小读操作的效率,以及它提供了很多附加的读取函数。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">r4</span> <span class="o">:=</span> <span class="nx">bufio</span><span class="p">.</span><span class="nf">NewReader</span><span class="p">(</span><span class="nx">f</span><span class="p">)</span>
<span class="nx">b4</span><span class="p">,</span> <span class="nx">err</span> <span class="o">:=</span> <span class="nx">r4</span><span class="p">.</span><span class="nf">Peek</span><span class="p">(</span><span class="mi">5</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">"5 bytes: %s\n"</span><span class="p">,</span> <span class="nb">string</span><span class="p">(</span><span class="nx">b4</span><span class="p">))</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>任务结束后要关闭这个文件
(通常这个操作应该在 <code>Open</code> 操作后立即使用 <code>defer</code> 来完成)。</p>
</td>
<td class="code">
<pre class="chroma">
<span class="nx">f</span><span class="p">.</span><span class="nf">Close</span><span class="p">()</span>
<span class="p">}</span>
</pre>
</td>
</tr>
</table>
<table>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="gp">$</span> echo "hello" > /tmp/dat
<span class="gp">$</span> echo "go" >> /tmp/dat
<span class="gp">$</span> go run reading-files.go
<span class="go">hello
</span><span class="go">go
</span><span class="go">5 bytes: hello
</span><span class="go">2 bytes @ 6: go
</span><span class="go">2 bytes @ 6: go
</span><span class="go">5 bytes: hello</span></pre>
</td>
</tr>
<tr>
<td class="docs">
<p>下面我们来看一下写入文件。</p>
</td>
<td class="code empty">
</td>
</tr>
</table>
<p class="next">
下一个例子: <a href="writing-files">写文件</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 \"io\"\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(' dat, err :\u003D os.ReadFile(\"/tmp/dat\")\u000A check(err)\u000A fmt.Print(string(dat))\u000A');codeLines.push(' f, err :\u003D os.Open(\"/tmp/dat\")\u000A check(err)\u000A');codeLines.push(' b1 :\u003D make([]byte, 5)\u000A n1, err :\u003D f.Read(b1)\u000A check(err)\u000A fmt.Printf(\"%d bytes: %s\\n\", n1, string(b1[:n1]))\u000A');codeLines.push(' o2, err :\u003D f.Seek(6, 0)\u000A check(err)\u000A b2 :\u003D make([]byte, 2)\u000A n2, err :\u003D f.Read(b2)\u000A check(err)\u000A fmt.Printf(\"%d bytes @ %d: \", n2, o2)\u000A fmt.Printf(\"%v\\n\", string(b2[:n2]))\u000A');codeLines.push(' o3, err :\u003D f.Seek(6, 0)\u000A check(err)\u000A b3 :\u003D make([]byte, 2)\u000A n3, err :\u003D io.ReadAtLeast(f, b3, 2)\u000A check(err)\u000A fmt.Printf(\"%d bytes @ %d: %s\\n\", n3, o3, string(b3))\u000A');codeLines.push(' _, err \u003D f.Seek(0, 0)\u000A check(err)\u000A');codeLines.push(' r4 :\u003D bufio.NewReader(f)\u000A b4, err :\u003D r4.Peek(5)\u000A check(err)\u000A fmt.Printf(\"5 bytes: %s\\n\", string(b4))\u000A');codeLines.push(' f.Close()\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>
</html>