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.
相关文章
- os.listdir()读出来的文件顺序和本地的顺序不一致,应该怎么做呢?
- Python OS 文件/目录方法
- 如何从Mac OS X Catalina将IPA文件上传到App Store
- ORA-48197: OS command to get the file status failed ORACLE 报错 故障修复 远程处理
- SQLServer 错误 833 SQL Server 已 %d 次遇到了针对数据库 [%ls] (%d) 中文件 [%ls] 的、所需完成时间超过 %d 秒的 I/O 请求。 OS 文件句柄是 0x%p。 最新的长时间 I/O 操作的偏移量是: %#016I64x。 故障 处理 修复 支持远程
- 令人惊喜:U盘安装Mac OS系统变得可能了!(u盘安装macos)
- MySQL Status Innodb_os_log_fsyncs 数据库状态作用意思及如何正确
- Python os.remove()函数:删除文件
- 王者荣耀登陆Mac OS平台(王者荣耀macos)
- os在Linux中安装Mac OS系统(linux下安装mac)
- Ubuntu 系统中通过火狐os模拟器轻松体验 Firefox OS
- Mac OS 2017:开启新纪元(macos2017)
- Pear OS 系统已成为历史!
- Alan Cox宣布Fuzix OS
- Chrome OS 马上可以运行所有安卓应用了
- 深入探索 OS X 与 Oracle 间的联系(osxoracle)
- Oracle OS认证:掌握一个安全的IT环境(oracleos认证)
- Exploring the Powerful Unix Roots of Mac OS: A Guide for Beginners(macosunix)
- python中os操作文件及文件路径实例汇总