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
    
  • 相关阅读:
    Xtreme9.0
    Xtreme8.0
    Xtreme8.0
    Xtreme8.0
    Xtreme9.0
    Xtreme8.0
    IEEEXtreme Practice Community Xtreme9.0
    MySQL添加用户、删除用户与授权
    程序员进阶之路—如何独当一面
    PowerDesigner版本控制器设置权限
  • 原文地址:https://www.cnblogs.com/boxker/p/10060824.html
Copyright © 2011-2022 走看看