Golang File Reading implementation method, golang File Reading
Read files
Put the Read file in file/test: that is, the test file under the file package. Write more files in it.
Method 1: Use ioutil. ReadFile to directly read from the file to [] byte
func Read0() (string){ f, err := ioutil.ReadFile("file/test") if err != nil { fmt.Println("read fail", err) } return string(f)}
Method 2: read from the file to the file, read from the file to the buf, and append the buf to the final [] byte
Func Read1 () (string) {// get a file f, err: = OS. Open ("file/test") if err! = Nil {fmt. println ("read fail") return "} // read the file to the buffer. close () var chunk [] byte buf: = make ([] byte, 1024) for {// read from file to buf n, err: = f. read (buf) if err! = Nil & err! = Io. EOF {fmt. println ("read buf fail", err) return ""} // indicates the end of reading. if n = 0 {break} // read to the final buffer. chunk = append (chunk, buf [: n]...)} return string (chunk) // fmt. println (string (chunk ))}
Method 3: Read the file from the file, read the file from the file to the Reader, read the buf from the Reader, and append the buf to the [] byte
// Read the file from the file, read the file from the file to the Reader, read the buf from the Reader, and append the buf to [] byte. This row is ranked third by func Read2 () (string) {fi, err: = OS. open ("file/test") if err! = Nil {panic (err)} defer fi. close () r: = bufio. newReader (fi) var chunks [] byte buf: = make ([] bytes, 1024) for {n, err: = r. read (buf) if err! = Nil & err! = Io. EOF {panic (err)} if 0 = n {break} // fmt. println (string (buf) chunks = append (chunks, buf ...)} return string (chunks) // fmt. println (string (chunks ))}
Method 4: Read the file and use ioutil to directly read the file to [] byte.
// Read the file and use ioutil to directly read the file to [] byte. This is the optimal func Read3 () (string) {f, err: = OS. open ("file/test") if err! = Nil {fmt. Println ("read file fail", err) return ""} defer f. Close () fd, err: = ioutil. ReadAll (f) if err! = Nil {fmt. Println ("read to fd fail", err) return ""} return string (fd )}
Read Speed comparison
According to my tests, the speed ranking of these four methods is: the former is excellent.
Method 4> method 1> method 3> Method 4
Write files
Method 1: write files using io. WriteString
Func Write0 () {fileName: = "file/test1" strTest: = "test" var f * OS. file var err error if CheckFileExist (fileName) {// File f, err = OS. openFile (fileName, OS. o_APPEND, 0666) // open the file if err! = Nil {fmt. println ("file open fail", err) return }} else {// the file does not exist f, err = OS. create (fileName) // Create a file if err! = Nil {fmt. Println ("file create fail") return }}// write the file into n, err1: = io. WriteString (f, strTest) if err1! = Nil {fmt. Println ("write error", err1) return} fmt. Println ("number of written Bytes:", n )}
Method 2: Use ioutil. WriteFile to write files
Func Write1 () {fileName: = "file/test2" strTest: = "test" var d = [] byte (strTest) err: = ioutil. writeFile (fileName, d, 0666) if err! = Nil {fmt. Println ("write fail")} fmt. Println ("write success ")}
Method 3: Use File (Write, WriteString) to Write files.
Func Write2 () {fileName: = "file/test3" strTest: = "test" var d1 = [] byte (strTest) f, err3: = OS. create (fileName) // Create the file if err3! = Nil {fmt. println ("create file fail")} defer f. close () n2, err3: = f. write (d1) // Write the file (byte array) fmt. printf ("write % d bytes n", n2) n3, err3: = f. writeString ("writesn") // write the file (byte array) fmt. printf ("write % d bytes n", n3) f. sync ()}
Method 4: Use bufio. NewWriter to write files
Func Write3 () {fileName: = "file/test3" f, err3: = OS. Create (fileName) // Create the file if err3! = Nil {fmt. println ("create file fail")} w: = bufio. newWriter (f) // create a Writer object n4, err3: = w. writeString ("bufferedn") fmt. printf ("write % d bytes n", n4) w. flush () f. close ()}
Check whether the file exists:
func CheckFileExist(fileName string) bool { _, err := os.Stat(fileName) if os.IsNotExist(err) { return false } return true}