This is a creation in Article, where the information may have evolved or changed.
File read/write
1.1 Copying files
package mainimport ( "os" "log" "io")func main(){ //打开原始文件 originalFile,err:=os.Open("test.txt") checkErr(err) defer originalFile.Close() //创建新的文件作为目标文件 newFile,err:=os.Create("test_copy.txt") checkErr(err) defer newFile.Close() //从源中复制字节到目标文件 bytesWritten,err:=io.Copy(newFile,originalFile) checkErr(err) log.Printf("copied %d bytes.",bytesWritten) // 将文件内容flush到硬盘中,好像没有这一步也可以 err=newFile.Sync() checkErr(err) }func checkErr(err error){ if err!=nil{ log.Fatal(err) }}
1.2 Jump to file specified location (seek)
package mainimport ( "os" "fmt" "log")func main() { file, _ := os.Open("test.txt") defer file.Close() // 偏离位置,可以是正数也可以是负数 var offset int64 = 5 // 用来计算offset的初始位置 // 0 = 文件开始位置 // 1 = 当前位置 // 2 = 文件结尾处 var whence int = 0 newPosition, err := file.Seek(offset, whence) if err != nil { log.Fatal(err) } fmt.Println("Just moved to 5:", newPosition) // 从当前位置回退两个字节 newPosition, err = file.Seek(-2, 1) if err != nil { log.Fatal(err) } fmt.Println("Just moved back two:", newPosition) // 使用下面的技巧得到当前的位置 currentPosition, err := file.Seek(0, 1) fmt.Println("Current position:", currentPosition) // 转到文件开始处 newPosition, err = file.Seek(0, 0) if err != nil { log.Fatal(err) } fmt.Println("Position after seeking 0,0:", newPosition)}
1.3 Writing Files
You can use
os
the package to write an open file.
Because
Go
executable packages are statically linked executables, each of your
import
packages will increase the size of your executable file. Other packages, such as,,
io
ioutil
bufio
provide some methods, but they are not required.
package mainimport ( "os" "log")func main() { //可写方式打开文件 file, err := os.OpenFile("test.txt", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666) checkErr(err) defer file.Close() //写字节到文件中 byteSlice := []byte("Bytes!\n") bytesWritten, err := file.Write(byteSlice) checkErr(err) log.Printf("wrote %d bytes.\n", bytesWritten)}func checkErr(err error) { if err != nil { log.Println(err) }}
1.4 Quick Write File
ioutil
packages have a very useful way
WriteFile()
to handle creating/opening files, writing sections,
slice
and closing files in a series of operations. If you need to write
节slice
to a file concisely and quickly, you can use it.
package mainimport ( "io/ioutil" "log")func main() { err := ioutil.WriteFile("test.txt", []byte("Hi\n"), 0666) if err != nil { log.Fatal(err) }}
1.5 Write with Cache
The
Bufio package provides writer with caching, so you can use the memory cache before writing bytes to the hard disk. It is useful when you are dealing with a lot of data, because it can save time to operate hard disk I/O. It can also be useful in other situations, such as writing one byte at a time, saving it in the memory cache, and then writing to the hard disk at once, reducing hard disk wear and improving performance.
Package Mainimport ("OS" "Log" "Bufio") func main () {//writable open file File,err:=os. OpenFile ("Test.txt", OS. o_wronly,0666) Checkerr (ERR) defer file. Close ()//Create buffered writer Bufferdwriter:=bufio for this file. Newwriter (file)//write yourself to BUF in Byteswritten,err:=bufferdwriter.write ([]byte{65,66,67}) Checkerr (err) log. Printf ("Bytes written:%d\n", Byteswritten)//write string to buffer//can also use Writerune () and WriteByte () byteswritten,err=bufferd Writer.writestring ("Buffered string\n") checkerr (Err) log. Printf ("bytes buffered:%d\n", Byteswritten)//Check the number of bytes in the cache unflushedbuffersize:=bufferdwriter.buffered () log. Printf ("Bytes buffered:%d\n", unflushedbuffersize)//How many bytes are available (unused cache size) bytesavailable:=bufferdwriter.available () Log. Printf ("Available buffer:%d\n", bytesavailable)//write memory buffer to hard drive bufferdwriter.flush ()//Discard the cached content that has not been Flush, clear the error and take it to the Output passed to the writer in the parameter//when you want to pass the cache to another writer useful bufferdwriter.reset (Bufferdwriter) Bytesavailable=bufferdwriter.avaIlable () Checkerr (ERR) log. Printf ("Available buffer:%d\n", bytesavailable)//Reset the size of the cache. The first parameter is where the cache should be exported, and in this case we use the same writer. If we set the new size less than the first parameter writer's cache size, such as 10, we will not get a 10 byte size cache,//But the original size of writer's cache, the default is 4096. Its function is mainly to expand the capacity. Bufio. Newwritersize (bufferdwriter,8000) bytesavailable= bufferdwriter.available () log. Printf ("Available buff:%d\n", bytesavailable)}func Checkerr (err error) {if err! = Nil {log. PRINTLN (ERR)}}
1.6 reads a maximum of n bytes
OS. File provides the basic functionality of the files operation, while Io, Ioutil, Bufio provide additional auxiliary functions.
package mainimport ( "os" "log")func main() { // 打开文件,只读 file, err := os.Open("test.txt") if err != nil { log.Fatal(err) } defer file.Close() // 从文件中读取len(b)字节的文件。 // 返回0字节意味着读取到文件尾了 // 读取到文件会返回io.EOF的error byteSlice := make([]byte, 16) bytesRead, err := file.Read(byteSlice) if err != nil { log.Fatal(err) } log.Printf("Number of bytes read: %d\n", bytesRead) log.Printf("Data read: %s\n", byteSlice)}
1.7 reads exactly n bytes
package mainimport ( "os" "log" "io")func main() { // Open file for reading file, err := os.Open("test.txt") if err != nil { log.Fatal(err) } // file.Read()可以读取一个小文件到大的byte slice中, // 但是io.ReadFull()在文件的字节数小于byte slice字节数的时候会返回错误 byteSlice := make([]byte, 2) numBytesRead, err := io.ReadFull(file, byteSlice) if err != nil { log.Fatal(err) } log.Printf("Number of bytes read: %d\n", numBytesRead) log.Printf("Data read: %s\n", byteSlice)}
1.8 read at least n bytes
package mainimport ( "os" "log" "io")func main() { // 打开文件,只读 file, err := os.Open("test.txt") if err != nil { log.Fatal(err) } byteSlice := make([]byte, 512) minBytes := 8 // io.ReadAtLeast()在不能得到最小的字节的时候会返回错误,但会把已读的文件保留 numBytesRead, err := io.ReadAtLeast(file, byteSlice, minBytes) if err != nil { log.Fatal(err) } log.Printf("Number of bytes read: %d\n", numBytesRead) log.Printf("Data read: %s\n", byteSlice)}
1.9 Read all bytes
package mainimport ( "os" "log" "fmt" "io/ioutil")func main() { file, err := os.Open("test.txt") if err != nil { log.Fatal(err) } // os.File.Read(), io.ReadFull() 和 // io.ReadAtLeast() 在读取之前都需要一个固定大小的byte slice。 // 但ioutil.ReadAll()会读取reader(这个例子中是file)的每一个字节,然后把字节slice返回。 data, err := ioutil.ReadAll(file) if err != nil { log.Fatal(err) } fmt.Printf("Data as hex: %x\n", data) fmt.Printf("Data as string: %s\n", data) fmt.Println("Number of bytes read:", len(data))}
1.10 Fast read to memory
package mainimport ( "log" "io/ioutil")func main() { // 读取文件到byte slice中 data, err := ioutil.ReadFile("test.txt") if err != nil { log.Fatal(err) } log.Printf("Data read: %s\n", data)}
1.11 Read with Cache
there are cache writes as well as cache reads.
Cache reader caches some of the content in memory. It will provide more than the OS. File and Io.reader more functions, the default cache size is 4096, the minimum cache is
Package Mainimport ("OS" "Log" "Bufio" "FMT") func main () {//Open file, create buffered reader file, err: = OS. Open ("test.txt") if err! = Nil {log. Fatal (Err)} BufferedReader: = Bufio. Newreader (file)//Get byte, current pointer invariant byteslice: = Make ([]byte, 5) byteslice, err = Bufferedreader.peek (5) If err! = Nil {log. Fatal (Err)} FMT. Printf ("peeked at 5 bytes:%s\n", byteslice)//read, pointer moves numbytesread at the same time, err: = Bufferedreader.read (Byteslice) if Err! = Nil {log. Fatal (Err)} FMT. Printf ("read%d bytes:%s\n", Numbytesread, Byteslice)//reads a byte, if the read is unsuccessful will return error mybyte, err: = Bufferedreader.read Byte () if err! = Nil {log. Fatal (Err)} FMT. Printf ("Read 1 byte:%c\n", MyByte)//Read to delimiter, contain delimiter, return byte slice databytes, err: = Bufferedreader.readbytes (' \ n ' If err! = Nil {log. Fatal (Err)} FMT. Printf ("Read bytes:%s\n", databytes)//Read to delimiter, contain delimiter, return string datastring, err: = BUFFeredreader.readstring (' \ n ') if err! = Nil {log. Fatal (Err)} FMT. Printf ("read string:%s\n", datastring)//This example reads a lot of lines, so test.txt should contain multiple lines of text without error}
3.12 Using scanner
Scanner
is the
bufio
type under the package, which is useful when working with delimited text in a file.
Usually we use a newline character as a delimiter to divide the contents of a file into multiple lines. In a CSV file, commas are generally used as separators.
Os. File files can be packaged into Bufio.scanner, which is like a cache reader.
We'll call the scan () method to read the next delimiter, using text () or bytes () to get the read data.
The delimiter can be not a simple byte or character, there is a special way to implement the function of the delimiter, and how much to move the pointer, what data is returned.
If no custom splitfunc is provided, the default scanlines uses the newline character as the delimiter, and the other separator functions include Scanrunes and scanwords, both in the Bufio package.
package mainimport ( "os" "log" "fmt" "bufio")func main() { file, err := os.Open("test.txt") if err != nil { log.Fatal(err) } scanner := bufio.NewScanner(file) // 缺省的分隔函数是bufio.ScanLines,我们这里使用ScanWords。 // 也可以定制一个SplitFunc类型的分隔函数 scanner.Split(bufio.ScanWords) // scan下一个token. success := scanner.Scan() if success == false { // 出现错误或者EOF是返回Error err = scanner.Err() if err == nil { log.Println("Scan completed and reached EOF") } else { log.Fatal(err) } } // 得到数据,Bytes() 或者 Text() fmt.Println("First word found:", scanner.Text()) // 再次调用scanner.Scan()发现下一个token}
Original: http://colobu.com/2016/10/12/...