参一:需要拷贝文件的路径
参二:拷贝到某个文件的路径
package main
import (
"fmt"
"io"
"os"
"syscall"
)
func CopyFile(path string, copyPath string) {
file, err := os.Open(path)
if err != nil {
fmt.Println("err = ", err)
return
}
copyFile, err := os.OpenFile(copyPath, syscall.O_RDWR|syscall.O_CREAT, 0777)
if err != nil {
fmt.Println("err = ", err)
return
}
defer file.Close()
defer copyFile.Close()
buff := make([]byte, 1024)
for {
n, err := file.Read(buff)
//fmt.Println(string(buff))
if err == io.EOF {
break
}
copyFile.Write(buff[0:n])
}
}
func main() {
fmt.Println(os.Args)
if len(os.Args) != 3 {
fmt.Println("Usage: -filename=[filename] -copyfilename=[copyfilename]")
return
}
fileName := os.Args[1]
copyFileName := os.Args[2]
fmt.Println(fileName)
fmt.Println(copyFileName)
CopyFile(fileName, copyFileName)
}