package main
import (
“os”
“fmt”
“bufio”
“io”
)
/*
说明将一张图片/电影/map3拷贝到另外一个文件e:/abc.jpg
//自己编写一个函数,接收两个文件路径srcFileName dstFileName
*/
func CopyFile(dstFileName string,srcFileName string)(written int64,err error){
srcfile,err:=os.Open(srcFileName)
if err!=nil{
fmt.Printf(“open file err=%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 err=%v\n", err)
return
}
//通过dstFile,获取到Writer
writer:=bufio.NewWriter(dstfile)
defer dstfile.Close()
return io.Copy(writer,reader)
}
func main() {
//d:/flower.jpg文件拷贝到e:/abc.jpg
//调用CopyFile完成文件拷贝
srcFile := “d:/flower.jpg”
dstFile := “e:/abc.jpg”
_,err :=CopyFile(dstFile,srcFile)
if err == nil{
fmt.Println(“拷贝完成”)
}else{
fmt.Printf(“拷贝错误err=%v”,err)
}
}