首页  

GO文件读取     所属分类 go 浏览量 816
os.File bufio ioutil
文件较小 可使用ioutil

bufio
const (
    defaultBufSize = 4096
)

func NewReader(rd io.Reader) *Reader {
    return NewReaderSize(rd, defaultBufSize)
}

func NewReaderSize(rd io.Reader, size int) *Reader


//使用File自带的Read func read1(filename string) int { fi, err := os.Open(filename) if err != nil { panic(err) } defer fi.Close() buf := make([]byte, 4096) var nbytes int for { n, err := fi.Read(buf) if err != nil && err != io.EOF { panic(err) } if n == 0 { break } nbytes += n } return nbytes } //使用bufio func read2(filename string) int { fi, err := os.Open(filename) if err != nil { panic(err) } defer fi.Close() buf := make([]byte, 4096) var nbytes int rd := bufio.NewReader(fi) for { n, err := rd.Read(buf) if err != nil && err != io.EOF { panic(err) } if n == 0 { break } nbytes += n } return nbytes } //使用ioutil func read3(filename string) int { fi, err := os.Open(filename) if err != nil { panic(err) } defer fi.Close() fd, err := ioutil.ReadAll(fi) nbytes := len(fd) return nbytes } func testfile1(filename string) { fmt.Printf("============test1 %s ===========\n", filename) start := time.Now() size1 := read1(filename) t1 := time.Now() fmt.Printf("Read 1 cost: %v, size: %d\n", t1.Sub(start), size1) size2 := read2(filename) t2 := time.Now() fmt.Printf("Read 2 cost: %v, size: %d\n", t2.Sub(t1), size2) size3 := read3(filename) t3 := time.Now() fmt.Printf("Read 3 cost: %v, size: %d\n", t3.Sub(t2), size3) }
bufio 读取一行数据 ReadBytes ReadString ReadSlice ReadLine 处理大文件时,ReadLine和ReadSlice效率相近,明显快于ReadString和ReadBytes ReadSlice 切片式读取,根据分隔符切片 ReadLine 在切片读取的基础上,对换行符\n和\r\n做了一些处理 func readline1(filename string) { fi, err := os.Open(filename) if err != nil { panic(err) } defer fi.Close() rd := bufio.NewReader(fi) for { _, err := rd.ReadBytes('\n') if err != nil || err == io.EOF { break } } } func readline2(filename string) { fi, err := os.Open(filename) if err != nil { panic(err) } defer fi.Close() rd := bufio.NewReader(fi) for { _, err := rd.ReadString('\n') if err != nil || err == io.EOF { break } } } func readline3(filename string) { fi, err := os.Open(filename) if err != nil { panic(err) } defer fi.Close() rd := bufio.NewReader(fi) for { _, err := rd.ReadSlice('\n') if err != nil || err == io.EOF { break } } } func readline4(filename string) { fi, err := os.Open(filename) if err != nil { panic(err) } defer fi.Close() rd := bufio.NewReader(fi) for { _, _, err := rd.ReadLine() if err != nil || err == io.EOF { break } } } func testfile2(filename string) { fmt.Printf("============test2 %s ===========\n", filename) start := time.Now() readline1(filename) t1 := time.Now() fmt.Printf("Readline 1 cost: %v\n", t1.Sub(start)) readline2(filename) t2 := time.Now() fmt.Printf("Readline 2 cost: %v\n", t2.Sub(t1)) readline3(filename) t3 := time.Now() fmt.Printf("Readline 3 cost: %v\n", t3.Sub(t2)) readline4(filename) t4 := time.Now() fmt.Printf("Readline 4 cost: %v\n", t4.Sub(t3)) }
完整测试代码 https://gitee.com/dyyx/hellocode/blob/master/web/tech/go/demo/fileread.go go run fileread.go -filename fileread.go filename= fileread.go ============test1 fileread.go =========== Read 1 cost: 79.591µs, size: 3616 Read 2 cost: 172.882µs, size: 3616 Read 3 cost: 113.971µs, size: 3616 ============test2 fileread.go =========== Readline 1 cost: 73µs Readline 2 cost: 73.806µs Readline 3 cost: 44.578µs Readline 4 cost: 35.342µs go run fileread.go -filename=fileread.go stat -filename=fileread.go: no such file or directory 为何 -filename=fileread.go 不行 ?
GO flag 命令行参数解析

上一篇     下一篇
go socket 编程 echo server

java HTTP Server NanoHttpd

java nio echo server

Java锁升级过程

GO内建函数

GO make 和 new的区别