```go
package main
import (
"bufio"
"fmt"
"io"
"os"
)
func main() {
srcFileName := "d:/abc.txt"
dstFileName := "d:/abcd.txt"
srcFile, err := os.Open(srcFileName)
if err != nil {
fmt.Println("open file err:", err)
return
}
reader := bufio.NewReader(srcFile)
dstFile, err := os.OpenFile(dstFileName, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
fmt.Println("write or create file err:", err)
return
}
writer := bufio.NewWriter(dstFile)
num, err := io.Copy(writer, reader)
if err == nil {
fmt.Println("完成拷贝字节数:", num)
} else {
fmt.Println("copy file err:", err)
return
}
defer srcFile.Close()
defer dstFile.Close()
}
```
执行后:显示操作的字节数是正确的,但目标文件内容和大小不正确,少了3K左右的字节,这是为什么?![问题.jpg](https://static.studygolang.com/191007/7ab5b992faf5ad6de4c133d84c994e2b.jpg)
有疑问加站长微信联系(非本文作者)