/** * Created with IntelliJ IDEA. * User: luosangnanka * Date: 13-7-16 * Time: 下午3:25 * To change this template use File | Settings | File Templates. */ package main import ( f "fmt" "io" "os" ) func main() { w, err := CopyFile("sb.log", "hellosb.log") if err != nil { f.Println(err.Error()) } f.Println(w) } func CopyFile(src, des string) (w int64, err error) { srcFile, err := os.Open(src) if err != nil { f.Println(err) } defer srcFile.Close() desFile, err := os.Create(des) if err != nil { f.Println(err) } defer desFile.Close() return io.Copy(desFile, srcFile) }
当目标文件内容和原文件不符之时 目标文件会被原文件覆盖。