zoukankan      html  css  js  c++  java
  • 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:UserscjigeDesktop	est.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
    
  • 相关阅读:
    vim 去掉自动注释和自动回车
    性别回归
    表情识别
    python list按字典的key值排序
    pytorch学习率策略
    python将list元素转为数字
    php面向对象
    mysql
    mysql
    mysql
  • 原文地址:https://www.cnblogs.com/boxker/p/10060824.html
Copyright © 2011-2022 走看看