Golang讀寫文件的幾種方式


 

BGbiao
42017.12.17 23:19:02字數 978閱讀 63,710

最近在使用Golang進行文件讀寫的過程中,遇到幾個細節問題導致程序寫入數據時有一定臟數據的殘留,最后發現是使用os.OpenFile在進行文件操作的時候沒有使用正確的flag造成的。因此專門去學習了下Golang中讀寫文件的幾種方式方法。

讀文件

使用golang語言去讀取一個文件默認會有多種方式,這里主要介紹以下幾種。

使用ioutil直接讀取

需要引入io/ioutil包,該包默認擁有以下函數供用戶調用。

func NopCloser(r io.Reader) io.ReadCloser func ReadAll(r io.Reader) ([]byte, error) func ReadDir(dirname string) ([]os.FileInfo, error) func ReadFile(filename string) ([]byte, error) func TempDir(dir, prefix string) (name string, err error) func TempFile(dir, prefix string) (f *os.File, err error) func WriteFile(filename string, data []byte, perm os.FileMode) error 

讀文件,我們可以看以下三個函數:

//從一個io.Reader類型中讀取內容直到返回錯誤或者EOF時返回讀取的數據,當err == nil時,數據成功讀取到[]byte中 //ReadAll函數被定義為從源中讀取數據直到EOF,它是不會去從返回數據中去判斷EOF來作為讀取成功的依據 func ReadAll(r io.Reader) ([]byte, error) //讀取一個目錄,並返回一個當前目錄下的文件對象列表和錯誤信息 func ReadDir(dirname string) ([]os.FileInfo, error) //讀取文件內容,並返回[]byte數據和錯誤信息。err == nil時,讀取成功 func ReadFile(filename string) ([]byte, error) 

讀取文件示例:

$ cat readfile.go package main import ( "fmt" "io/ioutil" "strings" ) func main() { Ioutil("mytestfile.txt") } func Ioutil(name string) { if contents,err := ioutil.ReadFile(name);err == nil { //因為contents是[]byte類型,直接轉換成string類型后會多一行空格,需要使用strings.Replace替換換行符 result := strings.Replace(string(contents),"\n","",1) fmt.Println(result) } } $ go run readfile.go xxbandy.github.io @by Andy_xu 

借助os.Open進行讀取文件

由於os.Open是打開一個文件並返回一個文件對象,因此其實可以結合ioutil.ReadAll(r io.Reader)來進行讀取。
io.Reader其實是一個包含Read方法的接口類型,而文件對象本身是實現了了Read方法的。

我們先來看下os.Open家族的相關函數

//打開一個需要被讀取的文件,如果成功讀取,返回的文件對象將可用被讀取,該函數默認的權限為O_RDONLY,也就是只對文件有只讀權限。如果有錯誤,將返回*PathError類型 func Open(name string) (*File, error) //大部分用戶會選擇該函數來代替Open or Create函數。該函數主要用來指定參數(os.O_APPEND|os.O_CREATE|os.O_WRONLY)以及文件權限(0666)來打開文件,如果打開成功返回的文件對象將被用作I/O操作 func OpenFile(name string, flag int, perm FileMode) (*File, error) 

使用os.Open家族函數和ioutil.ReadAll()讀取文件示例:

func OsIoutil(name string) { if fileObj,err := os.Open(name);err == nil { //if fileObj,err := os.OpenFile(name,os.O_RDONLY,0644); err == nil { defer fileObj.Close() if contents,err := ioutil.ReadAll(fileObj); err == nil { result := strings.Replace(string(contents),"\n","",1) fmt.Println("Use os.Open family functions and ioutil.ReadAll to read a file contents:",result) } } } # 在main函數中調用OsIoutil(name)函數就可以讀取文件內容了 $ go run readfile.go Use os.Open family functions and ioutil.ReadAll to read a file contents: xxbandy.github.io @by Andy_xu 

然而上述方式會比較繁瑣一些,因為使用了os的同時借助了ioutil,但是在讀取大文件的時候還是比較有優勢的。不過讀取小文件可以直接使用文件對象的一些方法。

不論是上邊說的os.Open還是os.OpenFile他們最終都返回了一個*File文件對象,而該文件對象默認是有很多方法的,其中讀取文件的方法有如下幾種:

//從文件對象中讀取長度為b的字節,返回當前讀到的字節數以及錯誤信息。因此使用該方法需要先初始化一個符合內容大小的空的字節列表。讀取到文件的末尾時,該方法返回0,io.EOF func (f *File) Read(b []byte) (n int, err error) //從文件的off偏移量開始讀取長度為b的字節。返回讀取到字節數以及錯誤信息。當讀取到的字節數n小於想要讀取字節的長度len(b)的時候,該方法將返回非空的error。當讀到文件末尾時,err返回io.EOF func (f *File) ReadAt(b []byte, off int64) (n int, err error) 

使用文件對象的Read方法讀取:

func FileRead(name string) { if fileObj,err := os.Open(name);err == nil { defer fileObj.Close() //在定義空的byte列表時盡量大一些,否則這種方式讀取內容可能造成文件讀取不完整 buf := make([]byte, 1024) if n,err := fileObj.Read(buf);err == nil { fmt.Println("The number of bytes read:"+strconv.Itoa(n),"Buf length:"+strconv.Itoa(len(buf))) result := strings.Replace(string(buf),"\n","",1) fmt.Println("Use os.Open and File's Read method to read a file:",result) } } } 

使用os.Openbufio.Reader讀取文件內容

bufio包實現了緩存IO,它本身包裝了io.Readerio.Writer對象,創建了另外的Reader和Writer對象,不過該種方式是帶有緩存的,因此對於文本I/O來說,該包是提供了一些便利的。

先看下bufio模塊下的相關的Reader函數方法:

//首先定義了一個用來緩沖io.Reader對象的結構體,同時該結構體擁有以下相關的方法 type Reader struct { } //NewReader函數用來返回一個默認大小buffer的Reader對象(默認大小好像是4096) 等同於NewReaderSize(rd,4096) func NewReader(rd io.Reader) *Reader //該函數返回一個指定大小buffer(size最小為16)的Reader對象,如果 io.Reader參數已經是一個足夠大的Reader,它將返回該Reader func NewReaderSize(rd io.Reader, size int) *Reader //該方法返回從當前buffer中能被讀到的字節數 func (b *Reader) Buffered() int //Discard方法跳過后續的 n 個字節的數據,返回跳過的字節數。如果0 <= n <= b.Buffered(),該方法將不會從io.Reader中成功讀取數據。 func (b *Reader) Discard(n int) (discarded int, err error) //Peekf方法返回緩存的一個切片,該切片只包含緩存中的前n個字節的數據 func (b *Reader) Peek(n int) ([]byte, error) //把Reader緩存對象中的數據讀入到[]byte類型的p中,並返回讀取的字節數。讀取成功,err將返回空值 func (b *Reader) Read(p []byte) (n int, err error) //返回單個字節,如果沒有數據返回err func (b *Reader) ReadByte() (byte, error) //該方法在b中讀取delimz之前的所有數據,返回的切片是已讀出的數據的引用,切片中的數據在下一次的讀取操作之前是有效的。如果未找到delim,將返回查找結果並返回nil空值。因為緩存的數據可能被下一次的讀寫操作修改,因此一般使用ReadBytes或者ReadString,他們返回的都是數據拷貝 func (b *Reader) ReadSlice(delim byte) (line []byte, err error) //功能同ReadSlice,返回數據的拷貝 func (b *Reader) ReadBytes(delim byte) ([]byte, error) //功能同ReadBytes,返回字符串 func (b *Reader) ReadString(delim byte) (string, error) //該方法是一個低水平的讀取方式,一般建議使用ReadBytes('\n') 或 ReadString('\n'),或者使用一個 Scanner來代替。ReadLine 通過調用 ReadSlice 方法實現,返回的也是緩存的切片,用於讀取一行數據,不包括行尾標記(\n 或 \r\n) func (b *Reader) ReadLine() (line []byte, isPrefix bool, err error) //讀取單個UTF-8字符並返回一個rune和字節大小 func (b *Reader) ReadRune() (r rune, size int, err error) 

示例:

func BufioRead(name string) { if fileObj,err := os.Open(name);err == nil { defer fileObj.Close() //一個文件對象本身是實現了io.Reader的 使用bufio.NewReader去初始化一個Reader對象,存在buffer中的,讀取一次就會被清空 reader := bufio.NewReader(fileObj) //使用ReadString(delim byte)來讀取delim以及之前的數據並返回相關的字符串. if result,err := reader.ReadString(byte('@'));err == nil { fmt.Println("使用ReadSlince相關方法讀取內容:",result) } //注意:上述ReadString已經將buffer中的數據讀取出來了,下面將不會輸出內容 //需要注意的是,因為是將文件內容讀取到[]byte中,因此需要對大小進行一定的把控 buf := make([]byte,1024) //讀取Reader對象中的內容到[]byte類型的buf中 if n,err := reader.Read(buf); err == nil { fmt.Println("The number of bytes read:"+strconv.Itoa(n)) //這里的buf是一個[]byte,因此如果需要只輸出內容,仍然需要將文件內容的換行符替換掉 fmt.Println("Use bufio.NewReader and os.Open read file contents to a []byte:",string(buf)) } } } 

讀取文件全部示例

/** * @File Name: readfile.go * @Author:Andy_xu @xxbandy.github.io * @Email:371990778@qq.com * @Create Date: 2017-12-16 16:12:01 * @Last Modified: 2017-12-17 12:12:02 * @Description:讀取指定文件的幾種方法,需要注意的是[]byte類型在轉換成string類型的時候,都會在最后多一行空格,需要使用result := strings.Replace(string(contents),"\n","",1) 方式替換換行符 */ package main import ( "fmt" "io/ioutil" "strings" "os" "strconv" "bufio" ) func main() { Ioutil("mytestfile.txt") OsIoutil("mytestfile.txt") FileRead("mytestfile.txt") BufioRead("mytestfile.txt") } func Ioutil(name string) { if contents,err := ioutil.ReadFile(name);err == nil { //因為contents是[]byte類型,直接轉換成string類型后會多一行空格,需要使用strings.Replace替換換行符 result := strings.Replace(string(contents),"\n","",1) fmt.Println("Use ioutil.ReadFile to read a file:",result) } } func OsIoutil(name string) { if fileObj,err := os.Open(name);err == nil { //if fileObj,err := os.OpenFile(name,os.O_RDONLY,0644); err == nil { defer fileObj.Close() if contents,err := ioutil.ReadAll(fileObj); err == nil { result := strings.Replace(string(contents),"\n","",1) fmt.Println("Use os.Open family functions and ioutil.ReadAll to read a file :",result) } } } func FileRead(name string) { if fileObj,err := os.Open(name);err == nil { defer fileObj.Close() //在定義空的byte列表時盡量大一些,否則這種方式讀取內容可能造成文件讀取不完整 buf := make([]byte, 1024) if n,err := fileObj.Read(buf);err == nil { fmt.Println("The number of bytes read:"+strconv.Itoa(n),"Buf length:"+strconv.Itoa(len(buf))) result := strings.Replace(string(buf),"\n","",1) fmt.Println("Use os.Open and File's Read method to read a file:",result) } } } func BufioRead(name string) { if fileObj,err := os.Open(name);err == nil { defer fileObj.Close() //一個文件對象本身是實現了io.Reader的 使用bufio.NewReader去初始化一個Reader對象,存在buffer中的,讀取一次就會被清空 reader := bufio.NewReader(fileObj) //使用ReadString(delim byte)來讀取delim以及之前的數據並返回相關的字符串. if result,err := reader.ReadString(byte('@'));err == nil { fmt.Println("使用ReadSlince相關方法讀取內容:",result) } //注意:上述ReadString已經將buffer中的數據讀取出來了,下面將不會輸出內容 //需要注意的是,因為是將文件內容讀取到[]byte中,因此需要對大小進行一定的把控 buf := make([]byte,1024) //讀取Reader對象中的內容到[]byte類型的buf中 if n,err := reader.Read(buf); err == nil { fmt.Println("The number of bytes read:"+strconv.Itoa(n)) //這里的buf是一個[]byte,因此如果需要只輸出內容,仍然需要將文件內容的換行符替換掉 fmt.Println("Use bufio.NewReader and os.Open read file contents to a []byte:",string(buf)) } } } 

寫文件

那么上述幾種方式來讀取文件的方式也支持文件的寫入,相關的方法如下:

使用ioutil包進行文件寫入

// 寫入[]byte類型的data到filename文件中,文件權限為perm func WriteFile(filename string, data []byte, perm os.FileMode) error 

示例:

$ cat writefile.go /** * @File Name: writefile.go * @Author: * @Email: * @Create Date: 2017-12-17 12:12:09 * @Last Modified: 2017-12-17 12:12:30 * @Description:使用多種方式將數據寫入文件 */ package main import ( "fmt" "io/ioutil" ) func main() { name := "testwritefile.txt" content := "Hello, xxbandy.github.io!\n" WriteWithIoutil(name,content) } //使用ioutil.WriteFile方式寫入文件,是將[]byte內容寫入文件,如果content字符串中沒有換行符的話,默認就不會有換行符 func WriteWithIoutil(name,content string) { data := []byte(content) if ioutil.WriteFile(name,data,0644) == nil { fmt.Println("寫入文件成功:",content) } } # 會有換行符 $ go run writefile.go 寫入文件成功: Hello, xxbandy.github.io! 

使用os.Open相關函數進行文件寫入

因為os.Open系列的函數會打開文件,並返回一個文件對象指針,而該文件對象是一個定義的結構體,擁有一些相關寫入的方法。

文件對象結構體以及相關寫入文件的方法:

//寫入長度為b字節切片到文件f中,返回寫入字節號和錯誤信息。當n不等於len(b)時,將返回非空的err func (f *File) Write(b []byte) (n int, err error) //在off偏移量出向文件f寫入長度為b的字節 func (f *File) WriteAt(b []byte, off int64) (n int, err error) //類似於Write方法,但是寫入內容是字符串而不是字節切片 func (f *File) WriteString(s string) (n int, err error) 

注意:使用WriteString()j進行文件寫入發現經常新內容寫入時無法正常覆蓋全部新內容。(是因為字符串長度不一樣)

示例:

//使用os.OpenFile()相關函數打開文件對象,並使用文件對象的相關方法進行文件寫入操作 func WriteWithFileWrite(name,content string){ fileObj,err := os.OpenFile(name,os.O_RDWR|os.O_CREATE|os.O_TRUNC,0644) if err != nil { fmt.Println("Failed to open the file",err.Error()) os.Exit(2) } defer fileObj.Close() if _,err := fileObj.WriteString(content);err == nil { fmt.Println("Successful writing to the file with os.OpenFile and *File.WriteString method.",content) } contents := []byte(content) if _,err := fileObj.Write(contents);err == nil { fmt.Println("Successful writing to thr file with os.OpenFile and *File.Write method.",content) } } 

注意:使用os.OpenFile(name string, flag int, perm FileMode)打開文件並進行文件內容更改,需要注意flag相關的參數以及含義。

const ( O_RDONLY int = syscall.O_RDONLY // 只讀打開文件和os.Open()同義 O_WRONLY int = syscall.O_WRONLY // 只寫打開文件 O_RDWR int = syscall.O_RDWR // 讀寫方式打開文件 O_APPEND int = syscall.O_APPEND // 當寫的時候使用追加模式到文件末尾 O_CREATE int = syscall.O_CREAT // 如果文件不存在,此案創建 O_EXCL int = syscall.O_EXCL // 和O_CREATE一起使用, 只有當文件不存在時才創建 O_SYNC int = syscall.O_SYNC // 以同步I/O方式打開文件,直接寫入硬盤. O_TRUNC int = syscall.O_TRUNC // 如果可以的話,當打開文件時先清空文件 ) 

使用io包中的相關函數寫入文件

io包中有一個WriteString()函數,用來將字符串寫入一個Writer對象中。

//將字符串s寫入w(可以是一個[]byte),如果w實現了一個WriteString方法,它可以被直接調用。否則w.Write會再一次被調用 func WriteString(w Writer, s string) (n int, err error) //Writer對象的定義 type Writer interface { Write(p []byte) (n int, err error) } 

示例:

//使用io.WriteString()函數進行數據的寫入 func WriteWithIo(name,content string) { fileObj,err := os.OpenFile(name,os.O_RDWR|os.O_CREATE|os.O_APPEND,0644) if err != nil { fmt.Println("Failed to open the file",err.Error()) os.Exit(2) } if _,err := io.WriteString(fileObj,content);err == nil { fmt.Println("Successful appending to the file with os.OpenFile and io.WriteString.",content) } } 

使用bufio包中的相關函數寫入文件

bufioio包中很多操作都是相似的,唯一不同的地方是bufio提供了一些緩沖的操作,如果對文件I/O操作比較頻繁的,使用bufio還是能增加一些性能的。

bufio包中,有一個Writer結構體,而其相關的方法也支持一些寫入操作。

//Writer是一個空的結構體,一般需要使用NewWriter或者NewWriterSize來初始化一個結構體對象 type Writer struct { // contains filtered or unexported fields } //NewWriterSize和NewWriter函數 //返回默認緩沖大小的Writer對象(默認是4096) func NewWriter(w io.Writer) *Writer //指定緩沖大小創建一個Writer對象 func NewWriterSize(w io.Writer, size int) *Writer //Writer對象相關的寫入數據的方法 //把p中的內容寫入buffer,返回寫入的字節數和錯誤信息。如果nn<len(p),返回錯誤信息中會包含為什么寫入的數據比較短 func (b *Writer) Write(p []byte) (nn int, err error) //將buffer中的數據寫入 io.Writer func (b *Writer) Flush() error //以下三個方法可以直接寫入到文件中 //寫入單個字節 func (b *Writer) WriteByte(c byte) error //寫入單個Unicode指針返回寫入字節數錯誤信息 func (b *Writer) WriteRune(r rune) (size int, err error) //寫入字符串並返回寫入字節數和錯誤信息 func (b *Writer) WriteString(s string) (int, error) 

注意:如果需要再寫入文件時利用緩沖的話只能使用bufio包中的Write方法

示例:

//使用bufio包中Writer對象的相關方法進行數據的寫入 func WriteWithBufio(name,content string) { if fileObj,err := os.OpenFile(name,os.O_RDWR|os.O_CREATE|os.O_APPEND,0644);err == nil { defer fileObj.Close() writeObj := bufio.NewWriterSize(fileObj,4096) // if _,err := writeObj.WriteString(content);err == nil { fmt.Println("Successful appending buffer and flush to file with bufio's Writer obj WriteString method",content) } //使用Write方法,需要使用Writer對象的Flush方法將buffer中的數據刷到磁盤 buf := []byte(content) if _,err := writeObj.Write(buf);err == nil { fmt.Println("Successful appending to the buffer with os.OpenFile and bufio's Writer obj Write method.",content) if err := writeObj.Flush(); err != nil {panic(err)} fmt.Println("Successful flush the buffer data to file ",content) } } } 

寫文件全部示例

/** * @File Name: writefile.go * @Author:Andy_xu @xxbandy.github.io * @Email:371990778@qq.com * @Create Date: 2017-12-17 12:12:09 * @Last Modified: 2017-12-17 23:12:10 * @Description:使用多種方式將數據寫入文件 */ package main import ( "os" "io" "fmt" "io/ioutil" "bufio" ) func main() { name := "testwritefile.txt" content := "Hello, xxbandy.github.io!\n" WriteWithIoutil(name,content) contents := "Hello, xuxuebiao\n" //清空一次文件並寫入兩行contents WriteWithFileWrite(name,contents) WriteWithIo(name,content) //使用bufio包需要將數據先讀到buffer中,然后在flash到磁盤中 WriteWithBufio(name,contents) } //使用ioutil.WriteFile方式寫入文件,是將[]byte內容寫入文件,如果content字符串中沒有換行符的話,默認就不會有換行符 func WriteWithIoutil(name,content string) { data := []byte(content) if ioutil.WriteFile(name,data,0644) == nil { fmt.Println("寫入文件成功:",content) } } //使用os.OpenFile()相關函數打開文件對象,並使用文件對象的相關方法進行文件寫入操作 //清空一次文件 func WriteWithFileWrite(name,content string){ fileObj,err := os.OpenFile(name,os.O_RDWR|os.O_CREATE|os.O_TRUNC,0644) if err != nil { fmt.Println("Failed to open the file",err.Error()) os.Exit(2) } defer fileObj.Close() if _,err := fileObj.WriteString(content);err == nil { fmt.Println("Successful writing to the file with os.OpenFile and *File.WriteString method.",content) } contents := []byte(content) if _,err := fileObj.Write(contents);err == nil { fmt.Println("Successful writing to thr file with os.OpenFile and *File.Write method.",content) } } //使用io.WriteString()函數進行數據的寫入 func WriteWithIo(name,content string) { fileObj,err := os.OpenFile(name,os.O_RDWR|os.O_CREATE|os.O_APPEND,0644) if err != nil { fmt.Println("Failed to open the file",err.Error()) os.Exit(2) } if _,err := io.WriteString(fileObj,content);err == nil { fmt.Println("Successful appending to the file with os.OpenFile and io.WriteString.",content) } } //使用bufio包中Writer對象的相關方法進行數據的寫入 func WriteWithBufio(name,content string) { if fileObj,err := os.OpenFile(name,os.O_RDWR|os.O_CREATE|os.O_APPEND,0644);err == nil { defer fileObj.Close() writeObj := bufio.NewWriterSize(fileObj,4096) // if _,err := writeObj.WriteString(content);err == nil { fmt.Println("Successful appending buffer and flush to file with bufio's Writer obj WriteString method",content) } //使用Write方法,需要使用Writer對象的Flush方法將buffer中的數據刷到磁盤 buf := []byte(content) if _,err := writeObj.Write(buf);err == nil { fmt.Println("Successful appending to the buffer with os.OpenFile and bufio's Writer obj Write method.",content) if err := writeObj.Flush(); err != nil {panic(err)} fmt.Println("Successful flush the buffer data to file ",content) } } }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM