当前位置:网站首页>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
边栏推荐
- Installation, use and problem summary of binlog2sql tool
- An 8266 crash
- Machine learning catalog
- Steps to build a deep learning environment GPU
- First experience of using fluent canvas
- Pit encountered using camera x_ When onpause, the camera is not released, resulting in a black screen when it comes back
- Data analysis learning directory
- Thoughts on the optimization of examination papers in the examination system
- 2021-2022-2 ACM集训队每周程序设计竞赛(8)题解
- The difference between ordinary inner class and static inner class
猜你喜欢

The fifth bullet of MySQL learning -- detailed explanation of transaction and its operation characteristics

Getting started with vcpkg

Sword finger offer II 116 Number of provinces - spatial complexity O (n), time complexity O (n)
![[报告] Microsoft :Application of deep learning methods in speech enhancement](/img/29/2d2addd826359fdb0920e06ebedd29.png)
[报告] Microsoft :Application of deep learning methods in speech enhancement

命令-sudo

I just want to leave a note for myself

開關電源設計分享及電源設計技巧圖解

MySQL syntax collation (5) -- functions, stored procedures and triggers

Application of DCT transform

Zero base to build profit taking away CPS platform official account
随机推荐
The flyer realizes page Jump through routing routes
Network protocol: SCTP flow control transmission protocol
机器学习目录
Using bafayun to control the computer
One stop service platform for high-level talents and development of comprehensive service platform system for talents
Executor、ExecutorService、Executors、ThreadPoolExecutor、Future、Runnable、Callable
Zero base to build profit taking away CPS platform official account
C1000k TCP connection upper limit test 1
openlayers draw矩形
Wechat applet part of the mobile phone Preview PDF did not respond
MySQL syntax collation (2)
高层次人才一站式服务平台开发 人才综合服务平台系统
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)
ArcGIS JS API dojoconfig configuration
Openharmony open source developer growth plan, looking for new open source forces that change the world!
Coordinate conversion WGS-84 to gcj-02 and gcj-02 to WGS-84
网络协议之:sctp流控制传输协议
[report] Microsoft: application of deep learning methods in speech enhancement
MySQL restores or rolls back data through binlog
Transaction processing of SQL Server database