Golang ioutil读写文件测试

运用 ioutil.ReadFile 、ioutil.WriteFile

package main

import (
	"io/ioutil"
	"log"
	"os"
)

func main() {

	buf, err := ioutil.ReadFile("C:/文档/MHOSO/conf/conf.txt")
	if err != nil {
		log.Println(err)
	}
	log.Println(string(buf))
	log.Println()

	//buf.append('8')
	/*newBuf := make([]byte, len(buf)*5)
	copy(newBuf, buf)
	log.Println(string(newBuf))
	log.Println()
	buf = newBuf*/

	str := []byte("this is the new word")
	log.Println(string(str))
	log.Println()

	buf = append(buf, str...)
	//buf := []byte("hi this is test")
	ioutil.WriteFile("C:/文档/MHOSO/conf/conf.txt", buf, os.ModeAppend)
	log.Println(string(buf))
}

*** 输出结果比较奇怪,不知道是不是内存问题:

[Running] go run "c:\Users\cjige\Desktop\test.go"
2018/12/03 20:50:11 hi this is test2018/12/03 20:50:11 hi this is test
[Done] exited with code=0 in 1.647 seconds

更换文件位置后,测试结果如下,append没有成功

[Running] go run "c:\文档\Go\学习\文件读写.go"
2018/12/03 20:54:17 hi this is test2018/12/03 20:54:17 
2018/12/03 20:54:17 this is the new word
2018/12/03 20:54:17 
2018/12/03 20:54:17 hi this is test
[Done] exited with code=0 in 1.542 seconds
posted @ 2018-12-03 20:52  波士地盘  阅读(1463)  评论(0编辑  收藏  举报