Golang 讀 寫檔案

2021-09-09 07:19:26 字數 4274 閱讀 3642

檔案的讀寫是程式語言的常見操作之一,這裡講一些goang 讀取檔案的相關操作。

讀取檔案有三種方式:

具體實現如下:

1、將檔案整個讀入記憶體

package main

import (

"os"

"io/ioutil"

"fmt"

)func main()

defer file.close()

content, err := ioutil.readall(file)

fmt.println(string(content))

}

或者

package main

import (

"os"

"io/ioutil"

"fmt"

)func main()

fmt.println(string(content))

}

將檔案整個讀入記憶體,效率比較高,占用記憶體也最高。

2、按位元組讀取檔案

package main

import (

"bufio"

"fmt"

"io"

"io/ioutil"

"os"

)func main()

defer fi.close()

r := bufio.newreader(fi)

chunks := make(byte, 0)

buf := make(byte, 1024) //一次讀取多少個位元組

for

fmt.println(string(buf[:n]))

break

if 0 == n

}fmt.println(string(chunks))

}

package main

import (

"fmt"

"io"

"os"

)func main()

defer f.close()

chunks := make(byte, 0)

buf := make(byte, 1024)

for

if 0 == n

}fmt.println(string(chunks))

}

3、按行讀取

package main

import (

"bufio"

"fmt"

"io"

"io/ioutil"

"os"

"strings"

)func main()

defer file.close()

stat, err := file.stat()

if err != nil

var size = stat.size()

fmt.println("file size=", size)

buf := bufio.newreader(file)

for else }}

}

有以下寫入方式

1、ioutil.writefile

package main

import (

"io/ioutil"

)func main()

}

這種方式每次都會覆蓋 test.txt內容,如果test.txt檔案不存在會建立。

2、os

package main

import (

"fmt"

"io"

"os"

)func checkfilei***ist(filename string) bool

return true

}func main() else

defer f.close()

n, err1 := io.writestring(f, wiretestring) //寫入檔案(字串)

if err1 != nil

fmt.printf("寫入 %d 個位元組n", n)

}

此種方法可以在檔案內容末尾新增新內容。

3、

package main

import (

"fmt"

"os"

)func checkfilei***ist(filename string) bool

return true

}func main() else

defer f.close()

n, err1 := f.write(byte(str)) //寫入檔案(位元組陣列)

fmt.printf("寫入 %d 個位元組n", n)

n, err1 = f.writestring(str) //寫入檔案(字串)

if err1 != nil

fmt.printf("寫入 %d 個位元組n", n)

f.sync()

}

此種方法可以在檔案內容末尾新增新內容。

4、bufio

package main

import (

"bufio"

"fmt"

"os"

)func checkfilei***ist(filename string) bool

return true

}func main() else

defer f.close()

if err1 != nil

w := bufio.newwriter(f) //建立新的 writer 物件

n, _ := w.writestring(str)

fmt.printf("寫入 %d 個位元組n", n)

w.flush()

}

此種方法可以在檔案內容末尾新增新內容。

package main

import (

"bufio"

"fmt"

"io"

"io/ioutil"

"os"

"time"

)func read0(path string) string

defer file.close()

content, err := ioutil.readall(file)

return string(content)

}func read1(path string) string

return string(content)

}func read2(path string) string

defer fi.close()

r := bufio.newreader(fi)

chunks := make(byte, 0)

buf := make(byte, 1024) //一次讀取多少個位元組

for

if 0 == n

}return string(chunks)

}func read3(path string) string

defer fi.close()

chunks := make(byte, 0)

buf := make(byte, 1024)

for

if 0 == n

}return string(chunks)

}func main()

執行結果:

第一次cost time 6.0003ms

cost time 3.0002ms

cost time 7.0004ms

cost time 11.0006ms

第二次cost time 7.0004ms

cost time 4.0003ms

cost time 6.0003ms

cost time 8.0005ms

第三次cost time 9.0006ms

cost time 3.0001ms

cost time 7.0004ms

cost time 11.0007ms

go lang 讀寫檔案操作

參考備份 寫程式離不了檔案操作,這裡總結下 go語言檔案操作。一 建立與開啟 建立檔案函式 func create name string file file,err error func newfile fd int,name string file 具體見官網 開啟檔案函式 func open ...

Golang 檔案讀寫操作

package main import fmt io log os path filepath strconv func main strfile strdir testfile.txt fmt.println file to open is strfile 開啟檔案,如果沒有,那麼建立,設定為讀寫...

golang 檔案讀寫彙總

檔案開啟os.open 返回唯讀模式的檔案描述符 os.openfile 0666 o rdonly 唯讀模式 read only o wronly 只寫模式 write only o rdwr 讀寫模式 read write o create 檔案不存在就建立 create a new file ...