Skip to main content

file operation

本章主要讲解文件的写入、追加、读取、复制操作

API详解

Go语言的 os 包下有一个 OpenFile 函数,其原型如下所示:

func OpenFile(name string, flag int, perm FileMode) (file *File, err error)

其中 name 是文件的文件名,如果不是在当前路径下运行需要加上具体路径;flag 是文件的处理参数,为 int 类型,根据系统的不同具体值可能有所不同,但是作用是相同的。

下面列举了一些常用的 flag 文件处理参数:

  • O_RDONLY:只读模式打开文件;
  • O_WRONLY:只写模式打开文件;
  • O_RDWR:读写模式打开文件;
  • O_APPEND:写操作时将数据附加到文件尾部(追加);
  • O_CREATE:如果不存在将创建一个新文件;
  • O_EXCL:和 O_CREATE 配合使用,文件必须不存在,否则返回一个错误;
  • O_SYNC:当进行一系列写操作时,每次都要等待上次的 I/O 操作完成再进行;
  • O_TRUNC:如果可能,在打开时清空文件。

文件的写入

【示例 1】:创建一个新文件 golang.txt,并在其中写入 5 句 "I love you, scott!"

package main

import (
"bufio"
"fmt"
"os"
)

func main() {
//创建一个新文件,写入内容 5 句 “http://c.biancheng.net/golang/”
filePath := "./test.txt"
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
fmt.Println("文件打开失败", err)
}
//及时关闭file句柄
defer file.Close()

//写入文件时,使用带缓存的 *Writer
writer := bufio.NewWriter(file)
for i := 0; i < 5; i++ {
writer.WriteString("I love you, scott!\n")
}
//Flush将缓存的文件真正写入到文件中
writer.Flush()
}

执行成功之后会在当前目录下生成一个 test.txt 文件,打开该文件如下图所示:

文件的追加

【示例 2】:打开一个存在的文件,在原来的内容追加内容“come on, scott”

package main

import (
"bufio"
"fmt"
"os"
)

func main() {
filePath := "./test.txt"
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
fmt.Println("文件打开失败", err)
}
//及时关闭file句柄
defer file.Close()

//写入文件时,使用带缓存的 *Writer
writer := bufio.NewWriter(file)
for i := 0; i < 5; i++ {
writer.WriteString("come on, scott \r\n")
}
//Flush将缓存的文件真正写入到文件中
writer.Flush()
}

执行成功之后,打开 test.txt 文件发现内容追加成功,如下图所示:

文件的读取

【示例 3】:打开一个存在的文件,将原来的内容读出来,显示在终端,并且追加 5 句“Hello,this sentence is appened”。

package main

import (
"bufio"
"fmt"
"io"
"os"
)

func main() {
filePath := "./test.txt"
file, err := os.OpenFile(filePath, os.O_RDWR|os.O_APPEND, 0666)
if err != nil {
fmt.Println("文件打开失败", err)
}
//及时关闭file句柄
defer file.Close()

//读原来文件的内容,并且显示在终端
reader := bufio.NewReader(file)
for {
str, err := reader.ReadString('\n')
if err == io.EOF {
break
}
fmt.Print(str)
}

//写入文件时,使用带缓存的 *Writer
writer := bufio.NewWriter(file)
for i := 0; i < 5; i++ {
writer.WriteString("Hello,this sentence is appened \r\n")
}
//Flush将缓存的文件真正写入到文件中
writer.Flush()
}

执行成功之后,会在控制台打印出文件的内容,并在文件中追加指定的内容,如下图所示:

文件逐行读取

package main

import (
"bufio"
"fmt"
"io"
"os"
"strings"
)

func main() {
fileName := "./a.txt"
file, err := os.OpenFile(fileName, os.O_RDWR, 0666)
if err != nil {
fmt.Println("Open file error!", err)
return
}
defer file.Close()

stat, err := file.Stat()
if err != nil {
panic(err)
}

var size = stat.Size()
fmt.Println("file size=", size)

buf := bufio.NewReader(file)
for {
line, err := buf.ReadString('\n')
line = strings.TrimSpace(line)
fmt.Println(line)
if err != nil {
if err == io.EOF {
fmt.Println("File read ok!")
break
} else {
fmt.Println("Read file error!", err)
return
}
}
}
}

运行结果

从Go 1.1开始,最新也是最简洁的方法就是用bufio.Scanner 请看示例

文件的复制

【示例 4】:编写一个程序,将一个文件的内容复制到另外一个文件(注:仅test.txt存在)

package main

import (
"fmt"
"io/ioutil"
)

func main() {
file1Path := "./test.txt"
file2Path := "./test2.txt"
data, err := ioutil.ReadFile(file1Path)
if err != nil {
fmt.Printf("文件打开失败=%v\n", err)
return
}
err = ioutil.WriteFile(file2Path, data, 0666)
if err != nil {
fmt.Printf("文件打开失败=%v\n", err)
}
}

执行成功后,发现内容已经复制成功,如下图所示:

utils

关于文件的常见操作,我已经封装成了API

使用时引入package即可

import (
"github.com/scott-x/gutils/fs"
)

API List:

  • func Dir() string: return the exec binary file folder
  • func RemoveAll(folder string): rm -rf
  • func RemoveFile(file string): remove one file
  • func Rename(oldName, newName string):rename the file or folder
  • func WriteBuf(file, data string) : wirte a string data into the file
  • func WriteString(file, data string) wirte a string data into the file
  • func Copy(from, to string) : copy file, must specify what to it is.
  • func CopyFile(srcName, dstName string) (written int64, err error):copy file
  • func CopyAndReplace(inputFile, outputFile string, replace map[string]string):copy and replace
  • func CreateDirIfNotExist(dir string): similar like mkdir -p /a/b/c
  • func CopyFolder(src, des string) error:copy folder, for example: CopyFolder("/Users/apple/go/src/github.com/scott-x/gutils", "/Users/apple/desktop/a") :copy all files from gutils to folder a, not include folder gutils
  • func IsExist(file string) bool: check file or folder if exists
  • func ReadAndReplace(file string, replace map[string]string) error: read file and replace it's content with map[string]string{"old1":"new1","old2":"new2"}
  • func ReadFile1(file string) (string, error): read the file and get the string content
  • func GetRunningFolder() string : get running folder, same as linux's command pwd
  • func GetEnv(env string) string:get the environment of the system
  • func NewFile(filePath string) : create file
  • func InsertAfter(insert *model.Insert):insert after with replace function
  • func InsertBefore(insert *model.Insert):insert before with replace function
  • func CheckFileType(file string) int: 0 indicates file, 1 indicates folder, -1 means doesn't exist.
  • func IsDirectory(path string) (bool, error) : if path doesn't exist or path is a file, it will return false.
  • func FileType(filename string) int:0 folder, 1 file, 0 error.
  • func List(folder string) []string : list the folder and files in current folder, doesn't loop inner folder.
  • func ListFiles(folder string) []string:just list the files in current folder
  • func ListFolder(folder string) []string:only list folder in current folder
  • func ListAll(folder string, ignore []string) ([]string, error): list all, but will ignore the matched substring in the full path.
  • func ListAllWithFileHeaders(folders []string) (*INFOS, int64, error):
  • func Zip(zipName string, Base string, files []string) : zip a file, Base will be removed.
  • func ZipWithBar(z *ZIP) : zip with progress bar
  • func Tab(n int) string: tabale n
  • func ReadJson(filename string) func(string) interface{}: read the configuration of json, return a method, with which we can get the related value.
  • func ModifyAttrOfJson_STRING(filename string, key string, value string): modify the string value
  • func ModifyAttrOfJson_FLOAT64(filename string, key string, value float64): modify the data of float64
  • func MD5(file string) string: md5 file checksum
  • func GetExpectedPath(folder,re string) string: it will loop the folder, and return the file/folder that 1st matches re, will return "" if not match.
  • func LastModify(pth string) string: get the time stamp that the file was last modified(string format)

Variable:

  • fs.HOME
  • fs.DESKTOP