在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
25.文件读写1.文件打开和读A.文件分类:文本和二进制文件 文件打开代码示例:1: package main2:3: import (4: "bufio"5: "fmt"6: "io"7: "os"8: )9:10: func main() {11: //只读的方式打开12: inputFile, err := os.Open("input.dat")13: if err != nil {14: fmt.Printf("open file err:%v\n", err)15: return16: }17: defer inputFile.Close()18: } 读文件file.Read和file.ReadAt. 读到文件末尾返回:io.EOF 读出整个文件代码示例 1: package main2:3: import (4: "fmt"5: "io"6: "os"7: )8:9: func main() {10: //只读的方式打开11: inputFile, err := os.Open(`D:\project\goland\files\wenjianduxie.go`)12: //如果你文件在GOPath里面,可以用下面的路径13: //inputFile, err := os.Open("./wenjianduxie.go")14: if err != nil {15: fmt.Printf("err=%v\n", err)16: return17: }18: defer func() {19: err := inputFile.Close()20: if err != nil {21: fmt.Println("err1=", err)22: }23: }()24: var buf [128]byte25: var content []byte26: for {27: n, err := inputFile.Read(buf[:])28: if err == io.EOF {29: // 代表文件读完了30: break31: }32: if err != nil {33: fmt.Println("err=", err)34: }35: fmt.Println("length:", n)36: // 将一个切片追加到另一个切片37: content = append(content, buf[:n]...)38: fmt.Println("data:\n", string(content))39: }40: } bufio原理由于文件读写相对内存读写性能较差,所以在读大文件时需要添加一层缓冲区,这就是bufio。 1: func main() {2: inputFile, err := os.Open("input.dat")3: if err != nil {4: fmt.Println("err=", err)5: return6: }7:8: defer inputFile.Close()9: inputReader := bufio.NewReader(inputFile)10: for {11: // 以换行符为分隔,一行一行打印出来。12: inputString, readerError :=inputReader.ReadString("\n")13: if readerError == io.EOF {14: return15: }16: fmt.Printf("The input was : %s", inputString)17: }18: } ioutil直接读取整个文件 1: func main() {2: inputFile := "products.txt"3: outputFile := "products_copy.txt"4: buf, err := ioutil.ReadFile(inputFile)5: if err != nil{6: fmt.Fprintf(os.Stderr, "File Error:%s\n", err)7: return8: }9: fmt.Printf("%s\n", string(buf))10: } 读取gzip压缩文件 1: func main() {2: zipfile := "Myfile.gz"3: //open里面也可以写路径4: fi, err1 := os.Open(zipfile)5: if err1 != nil {6: fmt.Fprintf(os.Stderr, "File Error:%s\n", err1)7: os.Exit(1)8: }9: fz, err2 := gzip.NewReader(fi)10: if err2 != nil {11: fmt.Fprintf(os.Stderr,"File Error:%s\n", err2)12: return13: }14: var r *bufio.Reader15: r = bufio.NewReader(fz)16: for {17: line, err := r.ReadString('\n')18: if err != nil {19: fmt.Println(“Done reading file”)20: os.Exit(0)21: }22: fmt.Println(line)23: }24: } 2.文件写入os.OpenFile("文件名",文件打开模式,权限控制) api1: file.Write()2: file.WriteAt()3: file.WriteString() bufio文件写入缓存区 1: writer := bufio.NewWriter() // 开辟缓存区2: writer.WriteString() // 写入缓存区3: writer.Flush() // 刷新,从缓存区写入文件 ioutil写入整个文件 1: inputFile := "旧文件"2: outputFile := "写入的新文件"3: buf, err := ioutil.ReadFile(inputFile)4: err = ioutil.WriteFile(outputFile, buf, 0x644) 3.defer原理图示执行顺序从上到下
|
请发表评论