加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Golang ioutil读写文件测试

发布时间:2020-12-16 09:31:51 所属栏目:大数据 来源:网络整理
导读:运用 ioutil.ReadFile 、ioutil.WriteFile package mainimport ( "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.app

运用 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:UserscjigeDesktoptest.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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读