Go語言程式設計中對檔案讀寫的基本方法整理

2022-09-24 21:12:10 字數 3454 閱讀 4113

1.func copy(dst writer, src reader) (written int64, err error)這個函式是從乙個檔案讀取拷貝到另外乙個檔案,一直拷貝到讀取檔案的eof,所以不會返回io.eof錯誤,引數是寫入目標器和讀取目標器,返回int64的拷貝位元組數和err資訊

複製** **如下:

import (

"fmt"

"io"

"os"

)func main()

fmt.println(num) //返回int64的11 開啟我的write.txt正是test.txt裡邊的hello widuu

} 2.func copyn(dst writer, src reader, n int64) (written int64, err error)看函式就知道了跟上述的是一樣的,只是多加了乙個讀取數的限制,然後我們看下**

複製** **如下:

import (

"fmt"

"io"

"io/ioutil"

"os"

)func main()

defer r.close()

b, _ := ioutil.readfile("write1.txt")

fmt.println(string(b)) //輸出 hello

fmt.println(num)       //5

}3.func readatleast(r reader, buf byte, min int) (n int, err error)這個函式就是從讀取器中讀取資料放到我們的buf中,限定了最小的讀取位元組數,如果我們讀取的資料小於最小讀取器,譬如你設定min的值是8,但是你讀取的資料位元組數是5就會返回乙個`io.errunexpectedeof`,如果大於就會返回`io.errshortbuffer`,讀取完畢會有`io.eof`~~,多講一些哈,這個reader只要我們滿足這個inte***ce就可以用這個

複製** **如下:

type reader inte***ce

其中*file就支援func (f *file) read(b byte) (n int, err error)

複製** **如下:

import (

"fmt"

"io"

"os"

)func main()

if err == io.errunexpectedeof

if err == io.errshortbuffer

if err == io.eof

total = total + n }}

4.func readfull(r reader, buf byte) (n int, err error)這個函式和上邊的函式是相似,只不過是讀取len(buf)個,放在buf中

複製** **如下:

import (

"fmt"

"io"

"os"

)func main()

if err == io.errunexpectedeof

fmt.println("read  value:", string(b)) //如果b是5 就出現這裡

}5.func writestring(w writer, s string) (n int, err error)弄完讀了,當然帶要寫了,這個函式程式設計客棧主要是向寫入目標中寫入字元創,返回是寫入的位元組數還有error錯誤,主要是許可權的錯誤,其中寫入呀!都是writer這個結構就可以寫入

複製** **如下:

type writer inte***ce

跟read一樣我們的*file是有func (f *file) write(b byte) (n int, err error),當然其實我們的*file中就已經有wirtestring了func (f *file) writestring(s string) (ret int, err error)

import (

"fmt"

"io"

"io/ioutil"

"os"

)func main()

defer w.close()

b, _ := ioutil.readfile("write1.txt")

fmt.println("write total", n) //write total 9

fmt.println(string(b))        // ni hao ma

}6.type limitedreader

複製** **如下:

type limitedreader struct

只實現了乙個方法func (l *limitedreader) read(p byte) (n int, err error)其實我們不難發現這個跟我們的readatlast()就是親兄弟的節奏

複製** **如下:

import (

"fmt"

"io"

"os"

)func main()

p := make(byte, 10)

var total int

for

total = total + n

7.type pipereader

複製** **如下:

type pipereader struct

(1)func pipe() (*pipereader, *pipewriter)建立乙個管道,並返回它的讀取器和寫入器,這個會在記憶體中進行管道同步,它的開啟會io.reader然後等待io.writer的輸入,沒有內部緩衝,它是安全的呼叫read和write彼此和並行呼叫寫

複製** **如下:

import (

"fmt"

&nblquydlfiusp;"io"

"reflect"

)func main()

(2)func (r *pipereader) close() error管道關閉後,正在進行或後續的寫入write操作返回errclosedpipe

複製** **如下:

import (

"fmt"

"io"

)func main()

}(3)func (r *pipereader) closewitherror(err error) error這個就是上邊的r.close關閉的時候,寫入器會返回錯誤的資訊

複製** **如下:

("errors"

"fmt"

"io"

)func main()

}(4)func (r *pipereader) read(data byte) (n int, err error)標準的閱讀介面,它從管道中讀取資料、阻塞一直到乙個寫入介面關閉,如果寫入端發生錯誤,它就會返回錯誤,否則返回的eof

複製** **如下:

import (

"fmt"

"io"

)func main()

本文位址:

python中對檔案的讀寫

檔案 將資料儲存到硬碟中 資料持久化 開啟檔案 open 檔案路徑,訪問模式 w write 寫入模式,只能寫,不能讀 f open 123.txt w 寫入資料 只能是字串 f.write hello world 關閉檔案 檔案操作完必須要關閉,否則檔案占用記憶體將無法釋放 記憶體洩漏 明知沒有用...

VC中對檔案的讀寫

注意 1 由於c是緩衝寫 所以要在關閉或重新整理後才能看到檔案內容 2 電腦處理文字型和二進位制型的不同 因為電腦只認識二進位制格式 方法一 用c 的方法 寫檔案 ofstream ofs 4.txt 如果我們要用這個類 我們就需要先 include ofs.write hello strlen h...

go語言的讀寫檔案

以create方法寫檔案 以新建的方式開啟,create方法每次開啟都會清空裡面的內容 f,err os.create hah.txt if err nil defer f.close f.writestring hello,kingsoft 以open唯讀的方式開啟 open是以唯讀的方式開啟,只...