当前位置:网站首页>Go three ways to copy files
Go three ways to copy files
2022-04-23 19:21:00 【Handsome that handsome】
Method 1 :io Under bag Read() and Write() Method realization
We can go through io Under bag Read() and Write() Method , Reading while writing , You can copy files . This method is to read the file by block , The size of the block will also affect the performance of the program
The function of this function : Copy files , The return value is the total number of copies ( byte ), error
func copyFile1(srcFile,destFile string)(int,error){
file1,err:=os.Open(srcFile)
if err != nil{
return 0,err
}
file2,err:=os.OpenFile(destFile,os.O_WRONLY|os.O_CREATE,os.ModePerm)
if err !=nil{
return 0,err
}
defer file1.Close()
defer file2.Close()
// Copy the data
bs := make([]byte,1024,1024)
n :=-1// Amount of data read
total := 0
for {
n,err = file1.Read(bs)
if err == io.EOF || n == 0{
fmt.Println(" Copy complete ..")
break
}else if err !=nil{
fmt.Println(" Wrong report ...")
return total,err
}
total += n
file2.Write(bs[:n])
}
return total,nil
}
Method 2 :io Under bag Copy() Method realization
We can also use io Under bag Copy() Method .
The sample code is as follows :
func copyFile2(srcFile, destFile string)(int64,error){
file1,err:=os.Open(srcFile)
if err != nil{
return 0,err
}
file2,err:=os.OpenFile(destFile,os.O_WRONLY|os.O_CREATE,os.ModePerm)
if err !=nil{
return 0,err
}
defer file1.Close()
defer file2.Close()
return io.Copy(file2,file1)
}
Method 3. ioutil Under bag ReadFile() and WriteFile() Method realization
func CopyFile2(srcFile, destFile string) (int, error) {
// Read all data in the target file at one time
content, err := ioutil.ReadFile(srcFile)
if err != nil {
fmt.Println("err:", err)
return 0, err
}
// Write the data in the target file to the specified file
err = ioutil.WriteFile(destFile, content, os.ModePerm)
if err != nil {
fmt.Println("err:", err)
return 0, err
}
return len(content),nil
}
版权声明
本文为[Handsome that handsome]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210557451159.html
边栏推荐
- 坐标转换WGS-84 转 GCJ-02 和 GCJ-02转WGS-84
- Redis core technology and practice 1 - start with building a simple key value database simplekv
- [报告] Microsoft :Application of deep learning methods in speech enhancement
- 网络协议之:sctp流控制传输协议
- Decompile and get the source code of any wechat applet - just read this (latest)
- Use of fluent custom fonts and pictures
- 什么是消息队列
- ArcMap connecting ArcGIS Server
- mysql通过binlog恢复或回滚数据
- Is meituan, a profit-making company with zero foundation, hungry? Coupon CPS applet (with source code)
猜你喜欢
The fifth bullet of MySQL learning -- detailed explanation of transaction and its operation characteristics
Common SQL commands
浅谈c语言指针的强制转换
Is meituan, a profit-making company with zero foundation, hungry? Coupon CPS applet (with source code)
JVM的类加载过程
Oracle configuration st_ geometry
Decompile and get the source code of any wechat applet - just read this (latest)
Esp01s with Arduino development environment
JVM的类加载过程
MySQL syntax collation (5) -- functions, stored procedures and triggers
随机推荐
On the forced conversion of C language pointer
Some records used by VS2010
Transaction processing of SQL Server database
An idea of rendering pipeline based on FBO
点云数据集常用处理
The most detailed network counting experiment in history (2) -- rip experiment of layer 3 switch
HTTP cache - HTTP authoritative guide Chapter VII
c1000k TCP 连接上限测试1
Openlayers 5.0 reload the map when the map container size changes
SSDB foundation 1
SSDB foundation 3
JS controls the file type and size when uploading files
Getting started with vcpkg
SSDB基础1
[记录]TypeError: this.getOptions is not a function
Strange passion
js 计算时间差
开关电源设计分享及电源设计技巧图解
Using oes texture + glsurfaceview + JNI to realize player picture processing based on OpenGL es
Reflection on the performance of some OpenGL operations in the past