1. os包
1.1 Write()/WriteString()方法
语法
打开文件
func OpenFile(name string, flag int, perm FileMode) (*File, error)
写入
func (f *File) WriteString(s string) (n int, err error)
或
func (f *File) Write(b []byte) (n int, err error)
完整示例
package main import ( "fmt" "os" ) func main() { info := "# 人员名单\r\n- 刘备刘玄德\r\n- 关羽关云长\r\n- 张飞张翼德" path := "nameList.md" writeFile(info,path) //readFile(path) } func writeFile(info string,path string) { fi, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0664) if err != nil { fmt.Println(err) return } defer fi.Close() _,err = fi.WriteString(info) if err != nil { return } //_,err = fi.Write([]byte(info)) //if err != nil { // return //} }
显示结果
1.2 Read()方法
语法
func (f *File) Read(b []byte) (n int, err error)
完整示例
package main import ( "fmt" "os" ) func main() { //info := "# 人员名单\r\n- 刘备刘玄德\r\n- 关羽关云长\r\n- 张飞张翼德" path := "nameList.md" //writeFile(info,path) readFile(path) } func readFile(path string) { fi, err := os.Open(path) if err != nil { fmt.Println( err) return } defer fi.Close() var tmp = make([]byte, 100) n,err := fi.Read(tmp) if err != nil { return } fmt.Printf("%s",tmp[:n]) }
结果显示
# 人员名单 - 刘备刘玄德 - 关羽关云长 - 张飞张翼德
2. bufio包
2.1 WriteString()/flush()方法
语法
创建writer实例
func NewWriter(w io.Writer) *Writer
将信息写入缓存
func (b *Writer) WriteString(s string) (int, error)
将缓冲写入文件
func (b *Writer) Flush() error
完整示例
import ( "bufio" "fmt" "io" "os" ) func main() { info := "# 人员名单\r\n- 刘备刘玄德\r\n- 关羽关云长\r\n- 张飞张翼德" path := "nameList.md" writeFile(info,path) } func writeFile(path string,info string) { fi, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0664) if err != nil { fmt.Println( err) return } defer fi.Close() //创建新Writer,其缓冲区有默认大小 writer := bufio.NewWriter(fi) //将信息写入缓存 _,err = writer.WriteString(info) if err != nil { return } //将缓存数据写入文件 err = writer.Flush() if err != nil { return } }
2.2 ReadLine()方法
语法
打开文件
func OpenFile(name string, flag int, perm FileMode) (*File, error)
创建reader
func NewReader(rd io.Reader) *Reader
读取结果
func (b *Reader) ReadLine() (line []byte, isPrefix bool, err error)
完整示例
package main import ( "bufio" "fmt" "io" "os" ) func main() { //info := "# 人员名单\r\n- 刘备刘玄德\r\n- 关羽关云长\r\n- 张飞张翼德" path := "nameList.md" //writeFile(info,path) readFile(path) } func readFile(path string) { fi, err := os.OpenFile(path,os.O_RDONLY,0664) if err != nil { fmt.Printf("Error: %s\n", err) return } defer fi.Close() reader := bufio.NewReader(fi) for { lineString, _,err := reader.ReadLine() if err == io.EOF { break } fmt.Println(string(lineString)) } }
结果
# 人员名单 - 刘备刘玄德 - 关羽关云长 - 张飞张翼德
3. ioutil 包
3.1 WriteFile()
语法
func WriteFile(filename string, data []byte, perm fs.FileMode) error
完整示例
package main import ( "fmt" "io/ioutil" ) func main() { info := "# 人员名单\r\n- 刘备刘玄德\r\n- 关羽关云长\r\n- 张飞张翼德" path := "nameList.md" writeFile(info,path) } func writeFile(info string,path string,) { err := ioutil.WriteFile(path,[]byte(info), 0664) if err != nil { fmt.Println(err) return } }
3.2 ReadFile()
func ReadFile(filename string) ([]byte, error)
完整示例
package main import ( "fmt" "io/ioutil" ) func main() { //info := "# 人员名单\r\n- 刘备刘玄德\r\n- 关羽关云长\r\n- 张飞张翼德" path := "nameList.md" readFile(path) } func readFile(path string,) { info,err := ioutil.ReadFile(path) if err != nil { fmt.Println(err) return } fmt.Printf("%s",info) }
结果
# 人员名单 - 刘备刘玄德 - 关羽关云长 - 张飞张翼德
标签:ioutil,info,WriteString,err,Write,func,path,os,string From: https://www.cnblogs.com/opensmarty/p/16798249.html