一、读取文件

二、文件写入

三、文件复制

 

1、普通版读取文件

Go 文件操作_htmlGo 文件操作_html_02
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版读取文件

Go 文件操作_htmlGo 文件操作_html_02
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)

Go 文件操作_htmlGo 文件操作_html_02
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、普通版文件写入

Go 文件操作_htmlGo 文件操作_html_02
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版文件写入

Go 文件操作_htmlGo 文件操作_html_02
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版文件写入

Go 文件操作_htmlGo 文件操作_html_02
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、文件复制

Go 文件操作_htmlGo 文件操作_html_02
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