当前位置:网站首页>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
边栏推荐
- Summary of several relationships of UML class diagram
- 数据分析学习目录
- The most detailed network counting experiment in history (2) -- rip experiment of layer 3 switch
- openlayers 5.0 当地图容器大小改变时,重新加载地图
- SSDB基础1
- Steps to build a deep learning environment GPU
- 深度学习环境搭建步骤—gpu
- 2022.04.23 (the best time for lc_714_to buy and sell stocks, including handling charges)
- Lottery applet, mother no longer have to worry about who does the dishes (assign tasks), so easy
- Gossip: on greed
猜你喜欢
MySQL syntax collation (2)
Audio signal processing and coding - 2.5.3 the discrete cosine transform
The difference between ordinary inner class and static inner class
Some records used by VS2010
浅谈c语言指针的强制转换
Client interns of a large factory share their experience face to face
JVM的类加载过程
Application of DCT transform
OpenHarmony开源开发者成长计划,寻找改变世界的开源新生力!
Intuitive understanding of the essence of two-dimensional rotation
随机推荐
Customize the non slidable viewpage and how to use it
为何PostgreSQL即将超越SQL Server?
How to uninstall easyton
Prefer composition to inheritance
RuntimeError: Providing a bool or integral fill value without setting the optional `dtype` or `out`
[record] typeerror: this getOptions is not a function
Pdf reference learning notes
2022.04.23 (the best time for lc_714_to buy and sell stocks, including handling charges)
2022.04.23 (lc_763_divided into letter interval)
MySQL syntax collation
OpenHarmony开源开发者成长计划,寻找改变世界的开源新生力!
Oracle配置st_geometry
Client interns of a large factory share their experience face to face
The platinum library cannot search the debug process records of some projection devices
OpenHarmony开源开发者成长计划,寻找改变世界的开源新生力!
JVM的类加载过程
Esp01s with Arduino development environment
Codeworks round 783 (Div. 2) d problem solution
The flyer realizes page Jump through routing routes
SSDB基础3