1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| package main
import ( "bufio" "fmt" "io" "os" )
func main() { srcfile, _ := os.OpenFile("~/in.txt", os.O_RDONLY, 0666) dstfile, _ := os.OpenFile("~/out.txt", os.O_CREATE|os.O_WRONLY, 0666) defer func() { srcfile.Close() dstfile.Close() fmt.Println("文件已关闭") }()
reader := bufio.NewReader(srcfile) writer := bufio.NewWriter(dstfile) buffer := make([]byte, 1024) for{ _ , err := reader.Read(buffer) if err != nil{ if err == io.EOF{ fmt.Println("源文件读取完毕") break }else { fmt.Println("error") return } }else { _, err1 := writer.Write(buffer) if err1 != nil{ fmt.Println(err1) return } }
} }
|