golang 逐行读取文件
本文内容纲要:
packagemain
import(
"bufio"
"fmt"
"io"
"os"
)
funcmain(){
fi,err:=os.Open("C:/DocumentsandSettings/xxx/Desktop/tax.txt")
iferr!=nil{
fmt.Printf("Error:%s\n",err)
return
}
deferfi.Close()
br:=bufio.NewReader(fi)
for{
a,_,c:=br.ReadLine()
ifc==io.EOF{
break
}
fmt.Println(string(a))
}
}
================一次性读取=====================
packagemain
import"fmt"
import"io/ioutil"
funcmain(){
b,e:=ioutil.ReadFile("d:/goTest/123.txt")
ife!=nil{
fmt.Println("readfileerror")
return
}
fmt.Println(string(b))
}
本文内容总结:
原文链接:https://www.cnblogs.com/rojas/p/4395866.html