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.

最新文章

  1. 用户信息 Froms验证票证
  2. Google Java编程风格指南中文版
  3. sqlserver 计算 百分比
  4. jq向上无缝滚动
  5. 解决EXC_BAD_ACCESS错误的一种方法--NSZombieEnabled
  6. Hadoop 新 MapReduce 框架 Yarn 详解
  7. MEF初体验之一:在应用程序宿主MEF
  8. 如何在sublime中使用sass
  9. promise入门demo
  10. Jupyter notebook 输出含中文的pdf 方法
  11. 在Cygwin中出现JAVA_HOME出现故障找不到出现故障
  12. XML的基礎結構
  13. c#发送短信
  14. String,StringBuffer和StringBuilder的区别
  15. Android实现录音的方法(最重要的是对MediaRecorder的试用方法)
  16. Scapy:局域网MAC地址扫描脚本
  17. linux svn 自启动
  18. linux 局域网探测工具nmap
  19. 【Python】使用python操作mysql数据库
  20. win8.1 win10存储设备和驱动器分开显示

热门文章

  1. Beta Edition [ Group 1 ]
  2. JS——滚动条
  3. 【转载】HTTP 基础与变迁
  4. java设计模式02观察者模式
  5. more
  6. linux安装mysql可视化工具MySQL-workbench 连接数据库 执行sql
  7. 文章或者观点说说等点赞功能实现(thinkphp)
  8. Python 模块的导入 day5
  9. fuel一键部署
  10. Capture the Flag ZOJ - 3879(模拟题)