Golang series: common file read and write operations

Go language provides a lot of support file operations, under different scenarios, there is a corresponding treatment, systematically comb today, several commonly used form of reading and writing files.

First, read the contents of the file

1, reads the file in bytes

This embodiment is in bytes read, the underlying relatively few, large amount of code, we see the following codes:

// read-bytes.go

package main

import (
    "fmt"
    "io"
    "os"
)

func main()  {
    file, _ := os.Open("test.txt")

    defer file.Close()

    // 字节切片缓存 存放每次读取的字节
    buf := make([]byte, 1024)

    // 该字节切片用于存放文件所有字节
    var bytes []byte

    for {
        // 返回本次读取的字节数
        count, err := file.Read(buf)

        // 检测是否到了文件末尾
        if err == io.EOF {
            break;
        }

        // 取出本次读取的数据
        currBytes := buf[:count]

        // 将读取到的数据 追加到字节切片中
        bytes = append(bytes, currBytes...)
    }

    // 将字节切片转为字符串 最后打印出来文件内容
    fmt.Println(string(bytes))
}

2, read in conjunction with ioutil

If we do not want to so much trouble, you can be combined ioutilto streamline the code above:

// read-with-util.go

package main

import (
    "fmt"
    "io/ioutil"
    "os"
)

func main()  {
    file, _ := os.Open("test.txt")

    defer file.Close()

    // ReadAll接收一个io.Reader的参数 返回字节切片
    bytes, _ := ioutil.ReadAll(file)

    fmt.Println(string(bytes))
}

Since os.File also realize io.Reader, we can call the ioutil.ReadAll(io.Reader)method, all files byte read out, the process of eliminating the use of byte caching read cycle.

3, only the package to complete the read operation ioutil:

To further simplify the document reading operation, ioutil also provides a ioutil.ReadFile(filename string)method of reading a line of code to get tasks:

// read-by-util.go

package main

import (
    "fmt"
    "io/ioutil"
)

func main()  {
    bytes, _ := ioutil.ReadFile("test.txt")

    fmt.Println(string(bytes))
}

4, line by line reads:

Sometimes in order to facilitate the analysis process, we hope to be able to read the contents of the file line by line, this time Scanner can be done:

// read-line-by-line.go

package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {
    file, _ := os.Open("test.txt")

    defer file.Close()

    // 接受io.Reader类型参数 返回一个bufio.Scanner实例
    scanner := bufio.NewScanner(file)

    var count int

    for scanner.Scan() {
        count++
        
        // 读取当前行内容
        line := scanner.Text()

        fmt.Printf("%d %s\n", count, line)
    }
}

This is simply a replica of java.util.Scanner Well, and Go language Scanner may also receive different input sources, such as os.Stdin and so on.

The above code directly print out the data of each line, if you want to get the contents of the final document, you can create a string slice, each time progressive scan, the content is appended to the current line sections can be.

These are some common file read mode, of course, there are other more advanced way, have the opportunity to do the summary.

Second, write file operation

1, the write operation is completed using ioutil

Above us the ioutil.ReadFile (filename string), with whom correspondence ioutil.WriteFile(filename string, ...)method, you can easily complete the write operation:

// write-by-util.go

package main

import (
    "io/ioutil"
)

func main() {
    data := []byte("hello goo\n")

    // 覆盖式写入
    ioutil.WriteFile("test.txt", data, 0664)
}

We see, WriteFile () method requires three arguments, it's full signature is: ioutil.WriteFile (filename string, data [] byte, perm os.FileMode). If the file does not exist, it is created under the authority of the specified file, if present, will first empty the contents of the original file, and then write new data.

Note that the last parameter, it is a 32-bit unsigned integer representing the rights of the current file is the standard Unix file permissions format.

Unix use -rwxrwxrwxthis form to represent file permissions, which:

  • No. 1: file attributes, -represents an ordinary file, drepresents a directory
  • 2-4 place: the file owner's permission
  • Bit 5-7: Permissions file belongs to user group
  • The first 8-10: other people's rights

In the permissions settings:

  • r It represents read, value 4
  • w Expressed write, value 2
  • x It represents exec, value 1

We adopted the following os.FileModetest:

package main

import (
    "fmt"
    "os"
)

func showMode(code int) {
    fmt.Println(os.FileMode(code).String())
}

func main() {
    showMode(0777)
    showMode(0766)
    showMode(0764)
}

Run the program, the console is printed as follows:

-rwxrwxrwx
-rwxrw-rw-
-rwxrw-r--

2, completing the write operation by File Handle

Above we have used os.Open (name string) method, which is open read-only files, os package also provides os.OpenFile (name string, flag int, perm FileMode) method, by specifying additional 读写方式and 文件权限parameter, the file operation become more flexible.

Wherein, flag several less common values:

  • os.O_CREATE: create if none exists does not exist, create
  • os.O_RDONLY: read-only read-only
  • os.O_WRONLY: write-only 只写
  • os.O_RDWR: read-write read and write
  • os.O_TRUNC: truncate when opened file length is cut 0: Clear File i.e.
  • os.O_APPEND: append append new data to the file

After opening the file, we can Write () and WriteString () method to write data, and finally through Sync () method to persist data to disk:

// write-by-file-descriptor.go

package main

import (
    "fmt"
    "os"
)

// 打印写入的字节数
func printWroteBytes(count int) {
    fmt.Printf("wrote %d bytes\n", count)
}

func main() {
    // 以指定的权限打开文件
    file, _ := os.OpenFile("test2.txt", os.O_RDWR | os.O_APPEND | os.O_CREATE, 0664)

    defer file.Close()

    data := []byte("hello go\n")

    // 写入字节
    count, _ := file.Write(data)

    printWroteBytes(count)

    // 写入字符串
    count, _ = file.WriteString("hello world\n")

    printWroteBytes(count)

    // 确保写入到磁盘
    file.Sync()
}

3, the writing operation is completed by the packet bufio

This way is in fact on the File handles made with a layer of packaging, direct and write call mode is very similar to the above, only be a reference:

// write-with-bufio.go

package main

import (
    "bufio"
    "fmt"
    "os"
)

func printWroteBytes(count int) {
    fmt.Printf("wrote %d bytes\n", count)
}

func main() {
    file, _ := os.OpenFile("test.txt", os.O_RDWR | os.O_APPEND | os.O_CREATE, 0664)

    defer file.Close()

    // 获取bufio.Writer实例
    writer := bufio.NewWriter(file)

    // 写入字符串
    count, _ := writer.Write([]byte("hello go\n"))

    fmt.Printf("wrote %d bytes\n", count)

    // 写入字符串
    count, _ = writer.WriteString("hello world\n")

    fmt.Printf("wrote %d bytes\n", count)

    // 清空缓存 确保写入磁盘
    writer.Flush()
}

These are commonly used file reading and writing, concluded today here, follow-up have the opportunity to explore more.

Guess you like

Origin www.cnblogs.com/liuhe688/p/11410464.html