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

在golang中截断文件

发布时间:2020-12-16 09:28:21 所属栏目:大数据 来源:网络整理
导读:截断文件时,似乎是在开头添加额外的零字节: configFile,err := os.OpenFile("./version.json",os.O_RDWR,0666)defer configFile.Close()check(err)//some actions happen hereconfigFile.Truncate(0)configFile.Write(js)configFile.Sync() 结果,文件具有我
截断文件时,似乎是在开头添加额外的零字节:

configFile,err := os.OpenFile("./version.json",os.O_RDWR,0666)
defer configFile.Close()
check(err)
//some actions happen here
configFile.Truncate(0)
configFile.Write(js)
configFile.Sync()

结果,文件具有我在开头用0字节的部分写的内容.

如何在没有前导零的情况下截断并完全重写文件?

解决方法

请参阅Truncate上的文档:

Truncate changes the size of the file. It does not change the I/O offset. If there is an error,it will be of type *PathError.

所以你还需要在写之前寻找文件的开头:

configFile.Truncate(0)
configFile.Seek(0,0)

(编辑:李大同)

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

    推荐文章
      热点阅读