go語言 檔案操作

2021-10-10 19:26:42 字數 4483 閱讀 2459

os.open()函式能夠開啟乙個檔案,返回乙個*file和乙個err。對得到的檔案例項呼叫close()方法能夠關閉檔案。

package main

import

("fmt"

"os"

)func

main()

// 關閉檔案

file.

close()

}

為了防止檔案忘記關閉,我們通常使用defer註冊檔案關閉語句。

read方法定義如下:

func

(f *file)

read

(b [

]byte

)(n int

, err error

)

它接收乙個位元組切片,返回讀取的位元組數和可能的具體錯誤,讀到檔案末尾時會返回0io.eof。 舉個例子:

func

main()

defer file.

close()

// 使用read方法讀取資料

var tmp =

make([

]byte

,128

) n, err := file.

read

(tmp)

if err == io.eof

if err !=

nil fmt.

printf

("讀取了%d位元組資料\n"

, n)

fmt.

println

(string

(tmp[

:n])

)}

使用for迴圈讀取檔案中的所有資料。

func

main()

defer file.

close()

// 迴圈讀取檔案

var content [

]byte

var tmp =

make([

]byte

,128

)for

if err !=

nil content =

(content, tmp[

:n]...)}

fmt.

println

(string

(content)

)}

bufio是在file的基礎上封裝了一層api,支援更多的功能。

package main

import

("bufio"

"fmt"

"io"

"os"

)// bufio按行讀取示例

func

main()

defer file.

close()

reader := bufio.

newreader

(file)

for fmt.

println

("檔案讀完了"

)break

}if err !=

nil fmt.

print

(line)

}}

io/ioutil包的readfile方法能夠讀取完整的檔案,只需要將檔名作為引數傳入。

package main

import

("fmt"

"io/ioutil"

)// ioutil.readfile讀取整個檔案

func

main()

fmt.

println

(string

(content)

)}

os.openfile()函式能夠以指定模式開啟檔案,從而實現檔案寫入相關功能。

func

openfile

(name string

, flag int

, perm filemode)

(*file,

error

)

其中:

name:要開啟的檔名flag:開啟檔案的模式。 模式有以下幾種:

模式含義

os.o_wronly只寫

os.o_create建立檔案

os.o_rdonly唯讀

os.o_rdwr讀寫

os.o_trunc清空

追加perm:檔案許可權,乙個八進位制數。r(讀)04,w(寫)02,x(執行)01。

func

main()

defer file.

close()

str :=

"hello"

file.

write([

]byte

(str)

)//寫入位元組切片資料

file.

writestring

("hello word"

)//直接寫入字串資料

}

func

main()

defer file.

close()

writer := bufio.

newwriter

(file)

for i :=

0; i <

10; i++

writer.

flush()

//將快取中的內容寫入檔案

}

func

main()

}

借助io.copy()實現乙個拷貝檔案函式。

// copyfile 拷貝檔案函式

func

copyfile

(dstname, srcname string

)(written int64

, err error

)defer src.

close()

// 以寫|建立的方式開啟目標檔案

dst, err := os.

openfile

(dstname, os.o_wronly|os.o_create,

0644

)if err !=

nildefer dst.

close()

return io.

copy

(dst, src)

//呼叫io.copy()拷貝內容

}func

main()

fmt.

println

("copy done!")}

func

copyfilebyself

(dstname, srcname string

)defer src.

close()

dst,err := os.

openfile

(dstname,os.o_create|os.o_wronly,

0644

)if err !=

nildefer dst.

close()

var tmp =

make([

]byte

,128

)for

if err !=

nil dst.

write

(tmp[

0:n])}

}func

copyfilebybuf

(dstname, srcname string

)defer src.

close()

dst,err := os.

openfile

(dstname,os.o_create|os.o_wronly,

0644

)if err !=

nildefer dst.

close()

reader := bufio.

newreader

(src)

writer := bufio.

newwriter

(dst)

forif err !=

nil writer.

writestring

(line)

writer.

flush()

}}

Go語言檔案操作

檔案的開啟和關閉 os.open 函式能夠開啟乙個檔案,返回乙個 file和乙個err。對得到的檔案例項呼叫close 方法能夠關閉檔案。為了防止檔案忘記關閉,我們通常使用defer註冊檔案關閉語句。讀取檔案 file.read read方法定義如下 func f file read b byte ...

Go語言檔案操作

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

Go語言檔案操作

輸入流和輸出流 os.file 封裝所有檔案相關操作,file是乙個結構體 官方文件 開啟檔案使用的是os包下的open,openfile函式 func open name string file file,err error open開啟乙個檔案用於讀取。如果操作成功,返回的檔案物件的方法可用於讀...