~~~
func copyFile(srcFileName string, dstFileName string) (written int64, err error) {
srcFile, err := os.Open(srcFileName)
if err != nil {
fmt.Printf("open file error = %v\n", err)
}
defer srcFile.Close()
//通过srcFile,获取到READER
reader := bufio.NewReader(srcFile)
//打开dstFileName
dstFile, err := os.OpenFile(dstFileName, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
fmt.Printf("open file error = %v\n", err)
return
}
//通过dstFile,获取到WRITER
writer := bufio.NewWriter(dstFile)
//writer.Flush()
defer dstFile.Close()
return io.Copy(writer, reader)
}
~~~
有疑问加站长微信联系(非本文作者)