|
|
@@ -154,6 +154,153 @@ filename := filepath.Base(path)
|
|
|
|
|
|
关于解析 CSV 文件,`encoding/csv` 包提供了相应的功能。具体请参考 [http://golang.org/pkg/encoding/csv/](http://golang.org/pkg/encoding/csv/)
|
|
|
|
|
|
+## 12.2.2 `compress`包:读取压缩文件
|
|
|
+
|
|
|
+`compress`包提供了读取压缩文件的功能,支持的压缩文件格式为:bzip2、flate、gzip、lzw 和 zlib。
|
|
|
+
|
|
|
+下面的程序展示了如何读取一个 gzip 文件。
|
|
|
+
|
|
|
+示例 12.7 [gzipped.go](examples/chapter_12/gzipped.go):
|
|
|
+```go
|
|
|
+package main
|
|
|
+
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "bufio"
|
|
|
+ "os"
|
|
|
+ "compress/gzip"
|
|
|
+)
|
|
|
+
|
|
|
+func main() {
|
|
|
+ fName := "MyFile.gz"
|
|
|
+ var r *bufio.Reader
|
|
|
+ fi, err := os.Open(fName)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Fprintf(os.Stderr, "%v, Can't open %s: error: %s\n", os.Args[0], fName,
|
|
|
+ err)
|
|
|
+ os.Exit(1)
|
|
|
+ }
|
|
|
+ fz, err := gzip.NewReader(fi)
|
|
|
+ if err != nil {
|
|
|
+ r = bufio.NewReader(fi)
|
|
|
+ } else {
|
|
|
+ r = bufio.NewReader(fz)
|
|
|
+ }
|
|
|
+
|
|
|
+ for {
|
|
|
+ line, err := r.ReadString('\n')
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("Done reading file")
|
|
|
+ os.Exit(0)
|
|
|
+ }
|
|
|
+ fmt.Println(line)
|
|
|
+ }
|
|
|
+}
|
|
|
+```
|
|
|
+
|
|
|
+## 12.2.3 写文件
|
|
|
+
|
|
|
+请看以下程序:
|
|
|
+
|
|
|
+示例 12.8 [fileoutput.go](examples/chapter_12/fileoutput.go):
|
|
|
+```go
|
|
|
+package main
|
|
|
+
|
|
|
+import (
|
|
|
+ "os"
|
|
|
+ "bufio"
|
|
|
+ "fmt"
|
|
|
+)
|
|
|
+
|
|
|
+func main () {
|
|
|
+ // var outputWriter *bufio.Writer
|
|
|
+ // var outputFile *os.File
|
|
|
+ // var outputError os.Error
|
|
|
+ // var outputString string
|
|
|
+ outputFile, outputError := os.OpenFile("output.dat", os.O_WRONLY|os.O_CREATE, 0666)
|
|
|
+ if outputError != nil {
|
|
|
+ fmt.Printf("An error occurred with file opening or creation\n")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer outputFile.Close()
|
|
|
+
|
|
|
+ outputWriter := bufio.NewWriter(outputFile)
|
|
|
+ outputString := "hello world!\n"
|
|
|
+
|
|
|
+ for i:=0; i<10; i++ {
|
|
|
+ outputWriter.WriteString(outputString)
|
|
|
+ }
|
|
|
+ outputWriter.Flush()
|
|
|
+}
|
|
|
+```
|
|
|
+
|
|
|
+除了文件句柄,我们还需要 `bufio` 的写入器。我们以只读模式打开文件 `output.dat`,如果文件不存在则自动创建:
|
|
|
+
|
|
|
+```go
|
|
|
+outputFile, outputError := os.OpenFile(“output.dat”, os.O_WRONLY|os.O_ CREATE, 0666)
|
|
|
+```
|
|
|
+
|
|
|
+可以看到,`OpenFile` 函数有三个参数:文件名、一个或多个标志(使用逻辑运算符“|”连接),使用的文件权限。
|
|
|
+
|
|
|
+我们通常会用到以下标志:
|
|
|
+`os.O_RDONLY`:只读
|
|
|
+`os.WRONLY`:只写
|
|
|
+`os.O_CREATE`:创建:如果指定文件不存在,就创建该文件。
|
|
|
+`os.O_TRUNC`:截断:如果指定文件已存在,就将该文件的长度截为0。
|
|
|
+
|
|
|
+在读文件的时候,文件的权限是被忽略的,所以在使用 `OpenFile` 时传入的第三个参数可以用0。而在写文件时,不管是 Unix 还是 Windows,都需要使用 0666。
|
|
|
+
|
|
|
+然后,我们创建一个写入器(缓冲区)对象:
|
|
|
+
|
|
|
+```go
|
|
|
+outputWriter := bufio.NewWriter(outputFile)
|
|
|
+```
|
|
|
+
|
|
|
+接着,使用一个 for 循环,将字符串写入缓冲区,写 10 次:`outputWriter.WriteString(outputString)`
|
|
|
+
|
|
|
+缓冲区的内容紧接着被完全写入文件:`outputWriter.Flush()`
|
|
|
+
|
|
|
+如果写入的东西很简单,我们可以使用 `fmt.Fprintf(outputFile, “Some test data.\n”)` 直接将内容写入文件。`fmt` 包里的 F 开头的 Print 函数可以直接写入任何 `io.Writer`,包括文件(请参考[章节12.8](12.8.md)).
|
|
|
+
|
|
|
+程序 `filewrite.go` 展示了不使用 `fmt.FPrintf` 函数,使用其他函数如何写文件:
|
|
|
+
|
|
|
+示例 12.8 [filewrite.go](examples/chapter_12/filewrite.go):
|
|
|
+```go
|
|
|
+package main
|
|
|
+
|
|
|
+import "os"
|
|
|
+
|
|
|
+func main() {
|
|
|
+ os.Stdout.WriteString("hello, world\n")
|
|
|
+ f, _ := os.OpenFile("test", os.O_CREATE|os.O_WRONLY, 0)
|
|
|
+ defer f.Close()
|
|
|
+ f.WriteString("hello, world in a file\n")
|
|
|
+}
|
|
|
+```
|
|
|
+
|
|
|
+使用 `os.Stdout.WriteString(“hello, world\n”)`,我们可以输出到屏幕。
|
|
|
+
|
|
|
+我们以只写模式创建或打开文件“test”,并且忽略了可能发生的错误:`f, _ := os.OpenFile(“test”, os.O_CREATE|os.O_WRONLY, 0)`
|
|
|
+
|
|
|
+我们不使用缓冲区,直接将内容写入文件:`f.WriteString( )`
|
|
|
+
|
|
|
+**练习 12.4**:[wiki_part1.go](exercises/chapter_12/wiki_part1.go)
|
|
|
+
|
|
|
+(这是一个独立的练习,但是同时也是为[章节15.4](15.4.md)做准备)
|
|
|
+
|
|
|
+程序中的数据结构如下,是一个包含以下字段的结构:
|
|
|
+
|
|
|
+```go
|
|
|
+type Page struct {
|
|
|
+ Title string
|
|
|
+ Body []byte
|
|
|
+}
|
|
|
+```
|
|
|
+
|
|
|
+请给这个结构编写一个 `save` 方法,将 Title 作为文件名、Body作为文件内容,写入到文本文件中。
|
|
|
+
|
|
|
+再编写一个 `load` 函数,接收的参数是字符串 title,该函数读取出与 title 对应的文本文件。请使用 *Page 做为参数,因为这个结构可能相当巨大,我们不想在内存中拷贝它。请使用 `ioutil` 包里的函数(参考章节12.2.1)。
|
|
|
+
|
|
|
|
|
|
## 链接
|
|
|
|