• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Go文件操作

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

一、读取文件

二、文件写入

三、文件复制

 

1、普通版读取文件

package main

import (
    "path/filepath"
    "os"
    "log"
    "io"
    "fmt"
)

func main() {

    path, err := filepath.Abs("./")
    if err != nil {
        panic(err)
    }

    filePath := path + "/day6/filehandler/openfile.go"

    file, err := os.Open(filePath)
    if err != nil {
        log.Printf("Open file: %s failed, err: %s\v", filePath, err)
        return
    }

    defer file.Close()

    var content []byte
    var buf[4096]byte

    for {
        n, err := file.Read(buf[:])
        if err != nil && err != io.EOF {
            log.Printf("read file: %s failed, err: %s\n", filePath, err)
            return
        }
        if err == io.EOF {
            break
        }

        fmt.Println("the buff n value is :", n)
        validBuf := buf[0:n]
        content = append(content, validBuf...)

    }
    
    fmt.Printf("the file content is: %s\n", content)
}
View Code

 

2、ioutil版读取文件

import (
    "path/filepath"
    "io/ioutil"
    "log"
    "fmt"
)

func main() {

    path, err := filepath.Abs("./")
    if err != nil {
        panic(err)
    }

    filePath := path + "/day6/filehandler/normalReadFile.go"
    contentBytes, err := ioutil.ReadFile(filePath)
    if err != nil {
        log.Printf("read file '%s' failed, err : %v\n", filePath, err)
        return
    }

    fmt.Printf("the file content: %s\n", contentBytes)
}
View Code

 

3、bufio版读取文件  ( bufio介绍 http://www.okyes.me/2016/05/30/go-bufio.html)

package main

import (
    "path/filepath"
    "os"
    "log"
    "bufio"
    "io"
    "fmt"
)

func main() {

    path, err := filepath.Abs("./")
    if err != nil {
        panic(err)
    }

    filePath := path + "/day6/filehandler/normalReadFile.go"
    file, err := os.Open(filePath)
    if err != nil {
        log.Printf("Open file: %s failed, err: %s\v", filePath, err)
        return
    }
    defer file.Close()

    reader := bufio.NewReader(file)
    var content []byte
    var buf[4096]byte
    for {
        n, err := reader.Read(buf[:])
        if err != nil && err != io.EOF {
            log.Printf("Read file '%s' failed, err: %s\v", filePath, err)
            return
        }
        if err == io.EOF {
            break
        }

        validBuf := buf[0:n]
        content = append(content, validBuf...)
    }

    fmt.Printf("the file content is: %s", content)

}
View Code

 

4、普通版文件写入

package main

import (
    "path/filepath"
    "os"
    "fmt"
)

func main() {

    pathStr, err := filepath.Abs("./")
    if err != nil {
        panic(err)
    }
    filePath := pathStr + "/day6/FileWrite/normalWriter"
    file, err := os.Create(filePath)
    if err != nil {
        panic(err)
    }

    contentByte := []byte("hello world!!!")
    n, err := file.Write(contentByte)
    if err != nil {
        panic(err)
    }
    fmt.Printf("write byte lenght %d", n)

}
View Code

 

5、ioutil版文件写入

package main

import (
    "path/filepath"
    "io/ioutil"
)

func main() {

    pathStr, err := filepath.Abs("./")
    if err != nil {
        panic(err)
    }
    filePath := pathStr + "/day6/FileWrite/ioutilWriter"

    data := []byte("IOUtil Hello World!!!")
    err = ioutil.WriteFile(filePath, data, 0666)
    if err != nil {
        panic(err)
    }
}
View Code

 

6、bufio版文件写入

package main

import (
    "path/filepath"
    "bufio"
    "os"
    "fmt"
)

func main() {

    pathStr, err := filepath.Abs("./")
    if err != nil {
        panic(err)
    }

    filePath := pathStr + "/day6/FileWrite/bufioWriter"
    file, err := os.Create(filePath)
    if err != nil {
        panic(err)
    }

    // writer := bufio.NewWriter(file)    默认的buffSize为4096
    writer := bufio.NewWriterSize(file, 512)
    dataByte := []byte("Bufio Hello World!!!")
    nn, err := writer.Write(dataByte)
    if err != nil {
        panic(err)
    }
    writer.Flush()
    fmt.Printf("write byte length :%d", nn)
}
View Code

 

7、文件复制

package main

import (
    "io"
    "os"
    "path/filepath"
    "log"
)

func main() {

    path, err := filepath.Abs("./")
    if err != nil {
        panic(err)
    }

    srcFilePath := path + "/day6/FileRead/normalReadFile.go"
    srcFile, err := os.Open(srcFilePath)
    if err != nil {
        log.Printf("Open file: %s failed, err: %s\v", srcFilePath, err)
        return
    }
    defer srcFile.Close()

    dstFilePath := path + "/day6/FileCopy/copyNormalReadFile.go"
    desFile, _ := os.OpenFile(dstFilePath, os.O_RDWR|os.O_CREATE, 0666)
    defer desFile.Close()

    io.Copy(desFile,srcFile)
}
View Code

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
go语言功能代码发布时间:2022-07-10
下一篇:
POJ1047RoundandRoundWeGo发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap