file句柄
os.Open(name string)os.OpenFile(name string, flag int, perm FileMode)读写方式文件权限
其中,flag 有以下几种常用的值:
- os.O_CREATE: create if none exists 不存在则创建
- os.O_RDONLY: read-only 只读
- os.O_WRONLY: write-only 只写
- os.O_RDWR: read-write 可读可写
- os.O_TRUNC: truncate when opened 文件长度截为0:即清空文件
- os.O_APPEND: append 追加新数据到文件
umask0022
06440755
读文件
func main() {
// 打开文件返回一个句柄
file, _ := os.Open("D:\\Desktop\\readme.txt")
defer file.Close()
// 缓冲区
buf := make([]byte, 1024)
// 存放文件所有内容
var bytes []byte
for {
// 每次读取一行放入缓冲区
count, err := file.Read(buf)
if err == io.EOF {
break
}
bytes = append(bytes, buf[:count]...)
}
fmt.Println(string(bytes))
}
写文件
Write()WriteString()Sync()
func main() {
file, _ := os.OpenFile("D:\\Desktop\\readme.txt", os.O_RDWR | os.O_APPEND | os.O_CREATE, 0644)
defer file.Close()
data := []byte("hello\nyes\nno\n")
// 写入字节
count, _ := file.Write(data)
fmt.Println("第一次写入", count, "字节")
// 写入字符串
count, _ = file.WriteString("write string\n")
fmt.Println("第二次写入", count, "字节")
// 确保写入磁盘
file.Sync()
}
更简单的ioutil
读文件
一次性读入所有内容:
func main() {
bytes, _ := ioutil.ReadFile("D:\\Desktop\\readme.txt")
fmt.Println(string(bytes))
}
逐行读入:
func main() {
file, _ := os.Open("D:\\Desktop\\readme.txt")
defer file.Close()
// 接受io.Reader类型参数 返回一个bufio.Scanner实例
scanner := bufio.NewScanner(file)
var count int
for scanner.Scan() {
count++
// 读取当前行内容
line := scanner.Text()
fmt.Println(count, ": " ,line)
}
}
写文件
覆盖写入:
func main() {
data := []byte("hello\nyes\nno\n")
ioutil.WriteFile("D:\\Desktop\\readme.txt", data, 0664)
}
追加:
func main() {
// 以指定的权限打开文件
file, _ := os.OpenFile("D:\\Desktop\\readme.txt", os.O_RDWR | os.O_APPEND | os.O_CREATE, 0664)
defer file.Close()
data := []byte("greet\n")
// 写入字节
count, _ := file.Write(data)
fmt.Println("第一次写入", count, "字节")
// 写入字符串
count, _ = file.WriteString("echo\n")
fmt.Println("第二次写入", count, "字节")
// 确保落盘
file.Sync()
}
io.Writer和io.Reader接口
type Writer interface {
Write(p []byte) (n int, err error)
}
type Writer interface {
Write(p []byte) (n int, err error)
}
os.Fileio.Copyio.WriteStringio.PipeWriter/Readerioutil
io.LimitReader函数
io.LimitReader
// LimitReader returns a Reader that reads from r
// but stops with EOF after n bytes.
// The underlying implementation is a *LimitedReader.
func LimitReader(r Reader, n int64) Reader { return &LimitedReader{r, n} }
下面演示一下基本的使用:
func main() {
file, _ := os.Open("D:\\Desktop\\readme.txt")
res := io.LimitReader(file, 20)
data := make([]byte, 1024)
count, err := res.Read(data)
if err != nil {
return
}
fmt.Println(string(data[:count]))
}
其中,res是一个Reader,且res只获取到了从文件开始往后20字节的内容。