2018-10-18 10:48
浏览 181

如何在Golang中删除大文件的前N个字节? [重复]

This question already has an answer here:

I have a big file about 10G, and i want to delete the first 10 bytes of this file.

If I use ioutil.ReadFile(), the whole file will be allocated to memory. This can't be done!

Another idea is that I read this file line by line. And after removing the data, I should write the remain data line by line. In this way, the memory can be saved, but if there exists a better way? Like split the reader or split the file?


图片转代码服务由CSDN问答提供 功能建议

此问题已经存在 在这里有答案

  • 从文本文件中删除前N个字节 1个答案 < / span>

    我有一个大约10G的大文件,我想删除前10个 此文件的字节数。

    如果我使用 ioutil.ReadFile(),则整个文件将分配给内存。

    另一个想法是我逐行读取此文件。 在删除数据之后,我应该逐行写入其余数据。 这样可以节省内存,但是是否存在更好的方法呢? 就像 reader split 文件的 split 一样?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doumeng2637
    doumeng2637 2018-10-18 10:55

    On most filesystems you can't "cut" a part out from the beginning or from the middle of a file, you can only truncate it at the end.

    The easiest to achieve what you want is to open the source file, skip the part you want to strip off (use seeking), open the destination file and simply copy from the source to the destination.

    For seeking (skipping), use File.Seek(). For copying between the files, use io.Copy().

    This is how it can be done:

    fin, err := os.Open("source.txt")
    if err != nil {
    defer fin.Close()
    fout, err := os.Create("dest.txt")
    if err != nil {
    defer fout.Close()
    // Offset is the number of bytes you want to exclude
    _, err = fin.Seek(10, io.SeekStart)
    if err != nil {
    n, err := io.Copy(fout, fin)
    fmt.Printf("Copied %d bytes, err: %v", n, err)

    Note that the above code will get the result file you want in a new file. If you want the "new" to be the old (meaning you don't want a different file), after the above operation (if it succeeds) delete the original file and rename the new one to the old.

    This is how you can do that final step:

    if err := os.Remove("source.txt"); err != nil {
    if err := os.Rename("dest.txt", "source.txt"); err != nil {
    点赞 评论