博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Golang学习日志 ━━ 实现io.copy的几种方式
阅读量:4116 次
发布时间:2019-05-25

本文共 2454 字,大约阅读时间需要 8 分钟。

golang自带copy文件功能,但是网上主要是有二种copy的方式,一种是利用系统自带的os.copy方法,一种是利用系统的read和write方法手动造轮实现copy功能,第三种是利用bufio的缓冲方式来实现。

最近的一次代码,利用copy方法,数据却死活写不进新文件。。。直到最后才发现是自己没写defer,提前把写文件给close()了,这种低级错误也是醉了,

实际使用的时候可以多种方式混合。举例如下:

//纯粹以手动读写方式实现copy功能func copyFilebyRW() {
fmt.Println("========拷贝文件============") c1, err := ioutil.ReadFile("./1.txt") if err != nil {
fmt.Println("读取错误") } err = ioutil.WriteFile("./2.txt", c1, 6044) if err != nil {
fmt.Println("录入失败") }}//读取,copy到write中func copyFileOStoOS() {
srcFile, err := os.Open("./1.txt") defer srcFile.Close() if err != nil {
fmt.Println("srcfile获取失败") return } dstFile, err := os.OpenFile("./2.txt", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644) defer dstFile.Close() if err != nil {
fmt.Println("dstfile获取失败") return } rtn, err := io.Copy(dstFile, srcFile) if err != nil {
fmt.Println("copy失败") } fmt.Println("copy结束:", rtn)}//读取缓冲,copy到不缓冲write中func copyFileBufftoOS() {
srcFile, err := os.Open("./1.txt") defer srcFile.Close() if err != nil {
fmt.Println("srcfile获取失败") return } srcBuf := bufio.NewReader(srcFile) dstFile, err := os.OpenFile("./2.txt", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644) defer dstFile.Close() if err != nil {
fmt.Println("dstfile获取失败") return } rtn, err := io.Copy(dstFile, srcBuf) if err != nil {
fmt.Println("copy失败") } fmt.Println("copy结束:", rtn)}//读取,copy到缓冲write中func copyFileOStoBuff() {
srcFile, err := os.Open("./1.txt") defer srcFile.Close() if err != nil {
fmt.Println("srcfile获取失败") return } dstFile, err := os.OpenFile("./2.txt", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644) defer dstFile.Close() if err != nil {
fmt.Println("dstfile获取失败") return } dstBuf := bufio.NewWriter(dstFile) rtn, err := io.Copy(dstBuf, srcFile) if err != nil {
fmt.Println("copy失败") } // 进入缓冲后输出,这步骤非常重要,否则虽然read成功了,但write文件为空 dstBuf.Flush() fmt.Println("copy结束:", rtn)}//读取缓冲,copy到缓冲write中func copyFileBufftoBuff() {
srcFile, err := os.Open("./1.txt") defer srcFile.Close() if err != nil {
fmt.Println("srcfile获取失败") return } srcBuf := bufio.NewReader(srcFile) dstFile, err := os.OpenFile("./2.txt", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644) defer dstFile.Close() if err != nil {
fmt.Println("dstfile获取失败") return } dstBuf := bufio.NewWriter(dstFile) rtn, err := io.Copy(dstBuf, srcBuf) if err != nil {
fmt.Println("copy失败") } // 进入缓冲后输出,这步骤非常重要,否则虽然read成功了,但write文件为空 dstBuf.Flush() fmt.Println("copy结束:", rtn)}

转载地址:http://xvkpi.baihongyu.com/

你可能感兴趣的文章
序列化与自定义序列化
查看>>
ThreadLocal
查看>>
从Executor接口设计看设计模式之最少知识法则
查看>>
OKhttp之Call接口
查看>>
application/x-www-form-urlencoded、multipart/form-data、text/plain
查看>>
关于Content-Length
查看>>
WebRequest post读取源码
查看>>
使用TcpClient可避免HttpWebRequest的常见错误
查看>>
EntityFramework 学习之一 —— 模型概述与环境搭建 .
查看>>
C# 发HTTP请求
查看>>
启动 LocalDB 和连接到 LocalDB
查看>>
Palindrome Number --回文整数
查看>>
Reverse Integer--反转整数
查看>>
Container With Most Water --装最多水的容器(重)
查看>>
Longest Common Prefix -最长公共前缀
查看>>
Letter Combinations of a Phone Number
查看>>
Single Number II --出现一次的数(重)
查看>>
Valid Parentheses --括号匹配
查看>>
Remove Element--原地移除重复元素
查看>>
Remove Duplicates from Sorted Array--从有序数组中移除重复元素
查看>>