golang文件读写三种方式——bufio,ioutil和os.create

简介:
复制代码
package main
import (
    "bufio"
    "fmt"
    "io/ioutil"
    "os"
)
func check(e error) {
    if e != nil {
        panic(e)
    }
}
func main() {
    d1 := []byte("hello\ngo\n")
    err := ioutil.WriteFile("/tmp/dat1", d1, 0644)
    check(err)
    f, err := os.Create("/tmp/dat2")
    check(err)
    defer f.Close()
    d2 := []byte{115, 111, 109, 101, 10}
    n2, err := f.Write(d2)
    check(err)
    fmt.Printf("wrote %d bytes\n", n2)
    n3, err := f.WriteString("writes\n")
    fmt.Printf("wrote %d bytes\n", n3)
    f.Sync()
    w := bufio.NewWriter(f)
    n4, err := w.WriteString("buffered\n")
    fmt.Printf("wrote %d bytes\n", n4)
    w.Flush()
}
复制代码

 

下面内容摘自:https://stackoverflow.com/questions/1821811/how-to-read-write-from-to-file-using-golang

Start with the basics

package main

import ( "io" "os" ) func main() { // open input file fi, err := os.Open("input.txt") if err != nil { panic(err) } // close fi on exit and check for its returned error defer func() { if err := fi.Close(); err != nil { panic(err) } }() // open output file fo, err := os.Create("output.txt") if err != nil { panic(err) } // close fo on exit and check for its returned error defer func() { if err := fo.Close(); err != nil { panic(err) } }() // make a buffer to keep chunks that are read buf := make([]byte, 1024) for { // read a chunk n, err := fi.Read(buf) if err != nil && err != io.EOF { panic(err) } if n == 0 { break } // write a chunk if _, err := fo.Write(buf[:n]); err != nil { panic(err) } } }

Here I used os.Open and os.Create which are convenient wrappers around os.OpenFile. We usually don't need to call OpenFile directly.

Notice treating EOF. Read tries to fill buf on each call, and returns io.EOF as error if it reaches end of file in doing so. In this case buf will still hold data. Consequent calls to Read returns zero as the number of bytes read and same io.EOF as error. Any other error will lead to a panic.

Using bufio

package main

import ( "bufio" "io" "os" ) 见链接

bufio is just acting as a buffer here, because we don't have much to do with data. In most other situations (specially with text files) bufio is very useful by giving us a nice API for reading and writing easily and flexibly, while it handles buffering behind the scenes.

Using ioutil

package main

import ( "io/ioutil" ) func main() { // read the whole file at once b, err := ioutil.ReadFile("input.txt") if err != nil { panic(err) } // write the whole body at once err = ioutil.WriteFile("output.txt", b, 0644) if err != nil { panic(err) } }

Easy as pie! But use it only if you're sure you're not dealing with big files.














本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/bonelee/p/6893398.html,如需转载请自行联系原作者

相关文章
|
6月前
|
Unix Go
Golang 语言的标准库 os 包怎么操作目录和文件?
Golang 语言的标准库 os 包怎么操作目录和文件?
27 0
|
3月前
|
Go
golang力扣leetcode 937.重新排列日志文件
golang力扣leetcode 937.重新排列日志文件
27 0
|
3月前
|
Go Windows
golang力扣leetcode 388.文件的最长绝对路径
golang力扣leetcode 388.文件的最长绝对路径
19 0
|
7月前
|
消息中间件 JSON 监控
golang读取conf文件的两种方式(ini和Viper)
golang读取conf文件的两种方式(ini和Viper)
157 0
|
9月前
|
Linux Go Windows
Golang-编译和Go语言文件基本机构
Go语言编译和Go语言文件基本机构
73 0
|
11月前
|
Go Perl
Golang每日一练(leetDay0066) 有效电话号码、转置文件
Golang每日一练(leetDay0066) 有效电话号码、转置文件
70 0
Golang:Excelize读写excel文件的Go语言库
Golang:Excelize读写excel文件的Go语言库
309 0
Golang:Excelize读写excel文件的Go语言库
|
测试技术 Go
Golang:os/io标准库读取文件
Golang:os/io标准库读取文件
88 0
|
数据可视化 测试技术 Go
golang 单测运行单个函数、文件、跳过文件命令
golang 单测运行单个函数、文件、跳过文件命令
|
Java Linux Go
知识分享之Golang——分享http包中常用的响应常量文件
知识分享之Golang篇是我在日常使用Golang时学习到的各种各样的知识的记录,将其整理出来以文章的形式分享给大家,来进行共同学习。欢迎大家进行持续关注。 知识分享系列目前包含Java、Golang、Linux、Docker等等。
128 0
知识分享之Golang——分享http包中常用的响应常量文件