文件读取
1.使用 ioutil.ReadFile 直接从文件读取
func ReadFileMethod1(fileName string) string {
f, err := ioutil.ReadFile(fileName)
if err != nil {
log.Printf("读取文件失败:%#v", err)
return ""
}
return string(f)
}
2.使用ioutil.ReadAll 将 file 直接读取
func ReadFileMethod2(fileName string) string{
f, err := os.Open(fileName)
if err != nil {
log.Printf("打开文件失败:%#v", err)
return ""
}
defer f.Close()
fd, err := ioutil.ReadAll(f)
if err != nil {
log.Printf("ioutil 读取文件失败:%#v", err)
return ""
}
return string(fd)
}
3.打开文件,读取到缓冲区,从缓冲区获取信息
func ReadFileMethod3(fileName string) string {
f, err := os.Open(fileName)
if err != nil {
log.Printf("read fail")
return ""
}
defer f.Close()
var dataBlock []byte
buf := make([]byte, 102400)
for {
n, err := f.Read(buf)
if err != nil {
if err == io.EOF{
break
}
log.Printf("读取缓冲数据失败:%#v", err)
return ""
}
dataBlock = append(dataBlock, buf[:n]...)
}
return string(dataBlock)
}
4.文件读取到file然后到Reader中,从Reader读取到缓冲区
func ReadFileMethod4(fileName string) string {
f, err := os.Open(fileName)
if err != nil {
log.Printf("打开文件失败:%#v", err)
}
defer f.Close()
r := bufio.NewReader(f)
var dataBlocks []byte
buf := make([]byte, 102400)
for {
n, err := r.Read(buf)
if err != nil {
if err == io.EOF {
break
}
log.Printf("打开文件失败:%#v", err)
}
dataBlocks = append(dataBlocks, buf[:n]...)
}
return string(dataBlocks)
}
对比读取耗时
func main() {
startTime := time.Now()
stringFile1 := ReadFileMethod1("test.dat")
endMethod1Time := time.Now()
stringFile2 := ReadFileMethod2("test.dat")
endMethod2Time := time.Now()
stringFile3 := ReadFileMethod3("test.dat")
endMethod3Time := time.Now()
stringFile4 := ReadFileMethod4("test.dat")
endMethod4Time := time.Now()
log.Printf("ReadFileMethod1 读取文件内容字节长度: %d 耗时: %s", len(stringFile1), endMethod1Time.Sub(startTime).String())
log.Printf("ReadFileMethod2 读取文件内容字节长度: %d 耗时: %s", len(stringFile2), endMethod2Time.Sub(endMethod1Time).String())
log.Printf("ReadFileMethod3 读取文件内容字节长度: %d 耗时: %s", len(stringFile3), endMethod3Time.Sub(endMethod2Time).String())
log.Printf("ReadFileMethod4 读取文件内容字节长度: %d 耗时: %s", len(stringFile4), endMethod4Time.Sub(endMethod3Time).String())
}
文件处理
1.判断文件是否存在
2.文件不存在就创建文件
func IsExists(path string) bool {
_, err := os.Stat(path)
if err != nil && !os.IsExist(err) {
return false
}
return true
}
func IfNoFileToCreate(fileName string) (file *os.File) {
var f *os.File
var err error
if !IsExists(fileName) {
f, err = os.Create(fileName)
if err != nil {
return
}
log.Printf("IfNoFileToCreate 函数成功创建文件:%s", fileName)
defer f.Close()
}
return f
}
文件写入
1. ioutil.WriteFile 写入文件
func WriteStringToFileMethod1(fileName string, writeInfo string) {
_ = IfNoFileToCreate(fileName)
info := []byte(writeInfo)
if err := ioutil.WriteFile(fileName, info, 0666); err != nil {
log.Printf("WriteStringToFileMethod1 写入文件失败:%+v", err)
return
}
log.Printf("WriteStringToFileMethod1 写入文件成功")
}
2. io.WriteString 写入文件
func WriteStringToFileMethod2(fileName string, writeInfo string) {
_ = IfNoFileToCreate(fileName)
f, err := os.OpenFile(fileName, os.O_APPEND, 0666)
defer f.Close()
if err != nil {
log.Printf("打开文件失败:%+v", err)
return
}
if _, err = io.WriteString(f, writeInfo); err != nil {
log.Printf("WriteStringToFileMethod2 写入文件失败:%+v", err)
return
}
log.Printf("WriteStringToFileMethod2 写入文件成功")
}
3. file.Write(WriteString) 写入文件
func WriteStringToFileMethod3(fileName string, writeInfo string) {
_ = IfNoFileToCreate(fileName)
f, err := os.OpenFile(fileName, os.O_APPEND, 0666)
defer f.Close()
if err != nil {
log.Printf("打开文件失败:%+v", err)
return
}
info := []byte(writeInfo)
if _, err = f.Write(info); err != nil {
log.Printf("WriteStringToFileMethod3 写入文件失败:%+v", err)
return
}
log.Printf("WriteStringToFileMethod3 写入文件成功")
}
4. bufio.NewWriter 写入文件
func WriteStringToFileMethod4(fileName string, writeInfo string) {
_ = IfNoFileToCreate(fileName)
f, err := os.OpenFile(fileName, os.O_APPEND, 0666)
defer f.Close()
if err != nil {
log.Printf("打开文件失败:%+v", err)
return
}
w := bufio.NewWriter(f)
if _, err = w.WriteString(writeInfo); err != nil {
log.Printf("WriteStringToFileMethod4 写入文件失败:%+v", err)
return
}
defer w.Flush()
log.Printf("WriteStringToFileMethod4 写入文件成功")
}
对比写入耗时
startWriteTime := time.Now()
WriteStringToFileMethod1("WriteStringToFileMethod1.dat", stringFile1)
endWriteMethod1Time := time.Now()
WriteStringToFileMethod2("WriteStringToFileMethod2.dat", stringFile1)
endWriteMethod2Time := time.Now()
WriteStringToFileMethod3("WriteStringToFileMethod3.dat", stringFile1)
endWriteMethod3Time := time.Now()
WriteStringToFileMethod4("WriteStringToFileMethod4.dat", stringFile1)
endWriteMethod4Time := time.Now()
log.Printf("WriteStringToFileMethod1 文件写入内容字节长度: %d 耗时: %s", len(stringFile1), endWriteMethod1Time.Sub(startWriteTime).String())
log.Printf("WriteStringToFileMethod2 文件写入内容字节长度: %d 耗时: %s", len(stringFile1), endWriteMethod2Time.Sub(endWriteMethod1Time).String())
log.Printf("WriteStringToFileMethod3 文件写入内容字节长度: %d 耗时: %s", len(stringFile1), endWriteMethod3Time.Sub(endWriteMethod2Time).String())
log.Printf("WriteStringToFileMethod4 文件写入内容字节长度: %d 耗时: %s", len(stringFile1), endWriteMethod4Time.Sub(endWriteMethod3Time).String())