[TOC] ## WriteFile - 写入文件 ```go ioutil.WriteFile(filename, data, 0666) ``` ## ReadAll - 获取实现`Read`接口的全部数据 ``` reader := strings.NewReader("helo") //常规 //p := make([]byte, 100) //reader.Read(p) //fmt.Println(string(p)) //h //获取所有 bytes, e := ioutil.ReadAll(reader) if e != nil { fmt.Println(e) } fmt.Println(string(bytes)) ``` ## ReadDir 读取目录下的文件 ``` infos, e := ioutil.ReadDir("./") if e != nil { fmt.Println(e) } for _, v := range infos { if v.IsDir() { fmt.Println(v.Name()) } } ``` ## ReadFile / WriteFile 函数 `bytes, e := ioutil.ReadFile("go.sum") ` `ioutil.WriteFile("test.txt", []byte("hello word"), os.ModePerm)` ## TempDir / TempFile 生成临时目录或文件 创建后记得删除 ``` if path, err := ioutil.TempDir("", "test-pre"); err == nil { fmt.Println(path) //C:\Users\idcpj\AppData\Local\Temp\test-pre202508787 defer os.RemoveAll(path) } ``` ``` if file, err := ioutil.TempFile("", "test-pre"); err == nil { fmt.Println(file.Name()) //C:\Users\idcpj\AppData\Local\Temp\test-pre933973611 defer func() { file.Close() os.Remove(file.Name()) }() } ```