go基础六 文件复制,断点续传

复制方法一:

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()
    //拷贝数据
    bs := make([]byte,1024,1024)
    n :=-1//读取的数据量
    total := 0
    for {
        n,err = file1.Read(bs)
        if err == io.EOF || n == 0{
            fmt.Println("拷贝完毕。。")
            break
        }else if err !=nil{
            fmt.Println("报错了。。。")
            return total,err
        }
        total += n
        file2.Write(bs[:n])
    }
    return total,nil

}

复制方法二

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)
}

复制方法三L:

func copyFile3(srcFile, destFile string)(int,error){
    input, err := ioutil.ReadFile(srcFile)
    if err != nil {
        fmt.Println(err)
        return 0,err
    }

    err = ioutil.WriteFile(destFile, input, 0644)
    if err != nil {
        fmt.Println("操作失败:", destFile)
        fmt.Println(err)
        return 0,err
    }

    return len(input),nil
}

 

文件断点续传:

package main

import (
    "strings"
    "fmt"
    "log"
    "os"
    "io"
    "strconv"
)

func main() {
    srcFile :="D:\\BaiduNetdiskDownload\\2019Golang进阶实战编程(学完基础必看)【千锋GO语言】\\2019版-千锋golang进阶-课件\\第08节_复制文件\\第8节_复制文件.md"
    s := srcFile[strings.LastIndex(srcFile,"\\")+1:]
    fmt.Println(s)
    tempFile :=s +"temp.md"
    fmt.Println(tempFile)
    //获取目标文件得file
    file, e := os.Open(srcFile)
    handle1(e)
    //获取输出文件得file
    openFile, e := os.OpenFile(s, os.O_WRONLY|os.O_CREATE, os.ModePerm)
    handle1(e)
    //获取临时文件得file
    file2, e := os.OpenFile(tempFile, os.O_CREATE|os.O_RDWR, os.ModePerm)
    handle1(e)
    defer file.Close()
    defer file2.Close()
    defer openFile.Close()
    // 读取临时文件的内容,设置从哪里开始
    // seek 方法,设置偏移量,后面参数距离开始,还是结束,还是当前文职
    file2.Seek(0,io.SeekStart)
    //设置读的字节数
    bs := make([]byte, 100, 100)
    //这个地方不需要处理异常
    n, err := file2.Read(bs)

    /*if err != nil{
        fmt.Println(err)
    }*/
    fmt.Println(n)
    countstr := string(bs[:n])

    fmt.Println(countstr)
    // 讲字符串转哈伟int类型去接受
    i, _ := strconv.ParseInt(countstr, 10, 64)
    fmt.Println(i)

    file.Seek(i,0)
    openFile.Seek(i,0)

    bytes := make([]byte, 1024, 1024)

    n1 :=-1
    n2 :=-1

    total :=int(i)

    for{
        n1, err = file.Read(bytes)
        if n1 ==0||err==io.EOF{
            fmt.Println("文件赋值完毕")
            file2.Close()
            os.Remove(tempFile)
            fmt.Println(total)
            break
        }
        n2, _ = openFile.Write(bytes[:n1])
        total +=n2
        file2.Seek(0,io.SeekCurrent)
        file2.WriteString(strconv.Itoa(n2))

        /*if total>800{
            panic("zhongduan")
        }*/

    }
}
func handle1(e error)  {
    if e !=nil {
        log.Fatal(e)
    }
}

 

你可能感兴趣的:(go基础六 文件复制,断点续传)