2
【代码】Go语言文件复制
source link: https://loli.fj.cn/2023/05/17/Go%E8%AF%AD%E8%A8%80%E6%96%87%E4%BB%B6%E5%A4%8D%E5%88%B6/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
【代码】Go语言文件复制
2023-05-17
Go语言文件复制
package main
import (
"bufio"
"io"
"os"
)
func CopyFile(sourceFileSrc string, targetFileSrc string) (written int64, err error) {
// 定义源文件对象
sourceFile, _ := os.Open("./a.txt")
defer sourceFile.Close()
// 文件输入流
reader := bufio.NewReader(sourceFile)
// 定义目标文件对象
targetFile, _ := os.OpenFile("./b.txt", os.O_WRONLY|os.O_CREATE, 0766)
defer targetFile.Close()
// 文件输出流
writer := bufio.NewWriter(targetFile)
// 文件复制
return io.Copy(writer, reader)
}
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK