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

转载于:https://www.cnblogs.com/boxker/p/10060824.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值