Go 编程实例【写文件】

阅读目录

Go 写文件和我们前面看过的读操作有着相似的方式。

// writing-files.go
package main

import (
	"bufio"
	"fmt"
	"io/ioutil"
	"os"
)

func check(e error) {
    
    
	if e != nil {
    
    
		panic(e)
	}
}

func main() {
    
    

	// 开始,这里是展示如何写入一个字符串(或者只是一些字节)到一个文件。
	d1 := []byte("hello\ngo\n")
	err := ioutil.WriteFile("/www/wwwroot/develop/work_test/test/writing-files.txt", d1, 0644)
	check(err)

	// 对于更细粒度的写入,先打开一个文件。
	f, err := os.Create("/www/wwwroot/develop/work_test/test/writing-files2.txt")
	check(err)

	// 打开文件后,习惯立即使用 defer 调用文件的 `Close` 操作。
	defer f.Close()

	// 你可以写入你想写入的字节切片
	d2 := []byte{
    
    115, 111, 109, 101, 10}
	n2, err := f.Write(d2)
	check(err)
	fmt.Printf("wrote %d bytes\n", n2)

	// `WriteString` 也是可用的。
	n3, err := f.WriteString("writes\n")
	fmt.Printf("wrote %d bytes\n", n3)

	// 调用 `Sync` 来将缓冲区的信息写入磁盘。
	f.Sync()

	// `bufio` 提供了和我们前面看到的带缓冲的读取器一样的带缓冲的写入器。
	w := bufio.NewWriter(f)
	n4, err := w.WriteString("buffered\n")
	fmt.Printf("wrote %d bytes\n", n4)

	// 使用 `Flush` 来确保所有缓存的操作已写入底层写入器。
	w.Flush()

}

[root@bogon test]# go run main.go 
wrote 5 bytes
wrote 7 bytes
wrote 9 bytes
[root@bogon test]# 

[root@bogon test]# ls -ll
total 20
-rw-r--r-- 1 root root   21 Mar 31 09:53 go.mod
-rw-r--r-- 1 root root 1270 Mar 31 10:37 main.go
-rw-r--r-- 1 root root    6 Mar 31 10:33 test.txt
-rw-r--r-- 1 root root   21 Mar 31 10:38 writing-files2.txt
-rw-r--r-- 1 root root    9 Mar 31 10:38 writing-files.txt
[root@bogon test]# cat writing-files.txt 
hello
go
[root@bogon test]# 

猜你喜欢

转载自blog.csdn.net/weiguang102/article/details/129874548