golang 读写文件内容(备份)
来源:互联网 发布:陈都灵 章泽天 知乎 编辑:程序博客网 时间:2024/06/08 01:06
以下是读取文件内容
os.O_RDONLY : the read flag for read-only access
os.WRONLY : the write flag for write-only access
os.O_CREATE : the create flag: create the file if it doesn’t exist
os.O_TRUNC : the truncate flag: truncate to size 0 if the file already exists
package mainimport ( "bufio" "fmt" "io" "os" "strings")func main() { fh, ferr := os.Open("d:\\n.txt") if ferr != nil { fmt.Printf("An error occurred on opening the inputfile\n" + "Does the file exist?\n" + "Have you got acces to it?\n") return } defer fh.Close() inputread := bufio.NewReader(fh) for { input, ferr := inputread.ReadString('\n') if ferr == io.EOF { return } fmt.Println(strings.TrimSpace(input)) }}
读取gzip格式文件:
package mainimport ("bufio""compress/gzip""fmt""os")func main() {fName := "MyFile.gz"var r *bufio.Readerfi, err := os.Open(fName)if err != nil {fmt.Fprintf(os.Stderr, "%v, Can't open %s: error: %s\n", os.Args[0],fName, err)os.Exit(1)}fz, err := gzip.NewReader(fi)if err != nil {r = bufio.NewReader(fi)//解压失败(还是读取原来文件)gz文件还是读取原始文件} else {r = bufio.NewReader(fz)//解压成功后读取解压后的文件}for {line, err := r.ReadString('\n')if err != nil {fmt.Println("Done reading file")os.Exit(0)}fmt.Println(line)}}
package mainimport ("bufio""fmt""os")func main() {outputFile, outputError := os.OpenFile("output.dat",os.O_WRONLY|os.O_CREATE, 0666)//0666是标准的权限掩码,关于打开标识看下面if outputError != nil {fmt.Printf("An error occurred with file creation\n")return}defer outputFile.Close()outputWriter := bufio.NewWriter(outputFile)outputString := "hello world!\n"for i := 0; i < 10; i++ {outputWriter.WriteString(outputString)}outputWriter.Flush()}
os.O_RDONLY : the read flag for read-only access
os.WRONLY : the write flag for write-only access
os.O_CREATE : the create flag: create the file if it doesn’t exist
os.O_TRUNC : the truncate flag: truncate to size 0 if the file already exists
- golang 读写文件内容(备份)
- golang文件读写
- 备份:nio文件读写
- 读写文件内容
- 读写文件,文件内容操作
- c# 读写ini文件内容
- xml文件内容的读写
- lua读写指定文件内容
- Autohotkey读写ini文件内容
- io流读写文件(文件内容修改)
- Go实战--golang中读写文件的几种方式
- Go实战--golang中读写文件的几种方式
- 2、文件读写、外部存储、Xml备份
- Golang不使用MD5比较文件内容是否一致
- Golang不使用MD5比较文件内容是否一致
- 【转】golang 文件操作 读写文件,上传文件,遍历文件,删除文件
- java读写删.text,.xml文件内容
- java读写中文内容的文件
- 第七周项目二:友元函数
- Mac OS X 编译第三方库 10.6.4 (64bits)
- 使用SDL播放PCM音频裸流
- java学习笔记【五】--thinking in java 小记录2
- jmagick处理图片,加水印
- golang 读写文件内容(备份)
- 非常简易,windows下安装ruby,非常easy
- 计算机硬件发展史
- openssh的密钥登录总结
- 执行ffmpeg1.2的build_libstagefright 脚本,找不到系统库。应该是工具链提供?
- 两个init方法的区别
- Android开发环境搭建
- android内存优化
- 用一个随机函数表示出更大范围的随机函数