创建文件
通过os包的Create、NewFile方法创建
func Create(name string)(file *File,err Error)
func NewFile(fd uintprt,name string)*File
打开文件
func Open(name string)(file *File,err Error)
func OpenFile(name string,flag int,perm uint32)(file *File,err Error)
写文件
func (file *File) Write(b []byte)(n int,err Error)
func (file *File)WriteAt(b []byte,off int64)(n int,err Error)
func (file *File)WriteString(s string)(ret int,err Error)
读文件
func (file *File) Read(b []byte)(n int,err Error)
func (file *File)ReadAt(b []byte,off int64)(n int,err Error)
f,err := os.Create(path)
defer f.Close()
var buf string
buf = fmt.Sprintf("你好")
n,err := f.WriteString(buf)
f,err := os.Create(path)
defer f.Close()
buf := make([] byte,1024*2)
n,err := f.Read(buf)
var s string
s = string(buf[:n])
bufio 按给定规则读取文件
r := bufio.NewReader(f)
buf,err := r.ReadBytes('\n')