💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
## 打开、关闭文件 ~~~ func main() { file, err := os.Open("./main.go") if err != nil { fmt.Println("open file failed") return } defer file.Close() fmt.Println("open file successfully") } ~~~ ## 读取文件内容 ~~~go func (f *File) Read(b []byte) (n int, err error) ~~~ ~~~ func main() { file, err := os.Open("./test.txt") if err != nil { fmt.Println("open file failed, err: ", err) return } defer file.Close() // Read方法来读取文件内容 // 声明长度为128的字节切片来存放读取到的数据 var tmp = make([]byte, 128) n, err := file.Read(tmp) if err == io.EOF { fmt.Println("文件读取完毕") return } if err != nil { fmt.Println("read file failed, err: ", err) return } fmt.Printf("读取了%d个字节\n", n) // 转成字符串 fmt.Println(string(tmp[:n])) } ~~~ ## 循环读取文件内容 ~~~ func readFromFile() { file, err := os.Open("./test.txt") if err != nil { fmt.Println("open file failed, err: ", err) return } defer file.Close() var content []byte var tmp = make([]byte, 128) for { n, err := file.Read(tmp) if err == io.EOF { fmt.Println("文件读取完毕") break } if err != nil { fmt.Println("read file failed, err: ", err) return } content = append(content, tmp[:n]...) } fmt.Println(string(content)) } ~~~ ## bufio读取文件 ~~~ func readFromFile() { file, err := os.Open("./test.txt") if err != nil { fmt.Println("open file failed, err: ", err) return } defer file.Close() reader := bufio.NewReader(file) for { line, err := reader.ReadString('\n') if err == io.EOF { if len(line) != 0 { fmt.Println(line) } fmt.Println("文件读取完毕!") break } if err != nil { fmt.Println("read file failed, err: ", err) return } fmt.Print(line) } } ~~~ ## ioutil读取文件 ~~~ func readFromFile() { content, err := ioutil.ReadFile("./test.txt") if err != nil { fmt.Println("read file failed, err: ", err) return } fmt.Println(string(content)) } ~~~ ## 写入文件 `os.OpenFile()`以指定模式打开文件,从而实现文件写入相关功能。 ~~~ const ( // Exactly one of O_RDONLY, O_WRONLY, or O_RDWR must be specified. O_RDONLY int = syscall.O_RDONLY // open the file read-only. O_WRONLY int = syscall.O_WRONLY // open the file write-only. O_RDWR int = syscall.O_RDWR // open the file read-write. // The remaining values may be or'ed in to control sbehavior. O_APPEND int = syscall.O_APPEND // append data to the file when writing. O_CREATE int = syscall.O_CREAT // create a new file if none exists. O_EXCL int = syscall.O_EXCL // used with O_CREATE, file must not exist. O_SYNC int = syscall.O_SYNC // open for synchronous I/O. O_TRUNC int = syscall.O_TRUNC // truncate regular writable file when opened. ) ~~~ **Write**和**WriteString** ~~~ func writeToFile() { file, err := os.OpenFile("./test.txt", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666) if err != nil { fmt.Println("open file failed, err: ", err) return } defer file.Close() str := "superman\n" file.Write([]byte(str)) file.WriteString("flash") } ~~~ **bufio.NewWriter** ~~~ func writeToFile() { file, err := os.OpenFile("./test.txt", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666) if err != nil { fmt.Println("open file failed, err: ", err) return } defer file.Close() writer := bufio.NewWriter(file) for i := 0; i < 10; i++ { writer.WriteString("hello world\n") // 将数据写入缓存 } writer.Flush() // 将缓存中的数据写入文件 ~~~ **ioutil.WriteFile** ~~~ func writeToFile() { str := "hello world" err := ioutil.WriteFile("./test.txt", []byte(str), 0666) if err != nil { fmt.Println("write file failed, err:", err) return } } ~~~ **拷贝文件** ~~~ func CopyFile(dstName, srcName string) (written int64, err error) { // open需要拷贝的源文件 src, err := os.Open(srcName) if err != nil { fmt.Printf("open %s failed, err:%v.\n", srcName, err) return } defer src.Close() // 写入目标文件 dst, err := os.OpenFile(dstName, os.O_WRONLY|os.O_CREATE, 0644) if err != nil { fmt.Printf("open %s failed, err:%v.\n", dstName, err) return } defer dst.Close() return io.Copy(dst, src) } ~~~