当前位置:网站首页>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
边栏推荐
- 開關電源設計分享及電源設計技巧圖解
- 点云数据集常用处理
- 开关电源设计分享及电源设计技巧图解
- Network protocol: SCTP flow control transmission protocol
- ArcMap连接 arcgis server
- Easy mock local deployment (you need to experience three times in a crowded time. Li Zao will do the same as me. Love is like a festival mock)
- SSDB基础3
- Use of fluent custom fonts and pictures
- Parsing headless jsonarray arrays
- Is meituan, a profit-making company with zero foundation, hungry? Coupon CPS applet (with source code)
猜你喜欢

First experience of using fluent canvas

Partage de la conception de l'alimentation électrique de commutation et illustration des compétences en conception de l'alimentation électrique

MySQL syntax collation (3)

Lottery applet, mother no longer have to worry about who does the dishes (assign tasks), so easy

NiO related Basics
![[record] typeerror: this getOptions is not a function](/img/c9/0d92891b6beec3d6085bd3da516f00.png)
[record] typeerror: this getOptions is not a function

Openharmony open source developer growth plan, looking for new open source forces that change the world!

FTP、ssh远程访问及控制

Some records used by VS2010

Prefer composition to inheritance
随机推荐
Keysight has chosen what equipment to buy for you
Thoughts on the optimization of examination papers in the examination system
MySQL syntax collation (4)
c1000k TCP 连接上限测试1
SQL Server database in clause and exists clause conversion
命令-sudo
Raspberry pie 18b20 temperature
Convert string to JSON
An 8266 crash
Strange problems in FrameLayout view hierarchy
Intuitive understanding of the essence of two-dimensional rotation
MySQL syntax collation (3)
[advanced level 11 of C language -- character and string functions and their simulation implementation (2)]
Openlayers 5.0 discrete aggregation points
JS to get the local IP address
点云数据集常用处理
JS calculation time difference
Why is PostgreSQL about to surpass SQL Server?
mysql通过binlog恢复或回滚数据
浅谈c语言指针的强制转换