1 Star 1 Fork 0

D10.天地弦 / gobase

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
cachefile.go 1.34 KB
一键复制 编辑 原始数据 按行查看 历史
D10.天地弦 提交于 2024-04-30 17:12 . * 读取文件大小
package gobase
import (
"bufio"
"os"
)
type CacheFile struct {
filename string
fh *os.File
fw *bufio.Writer
cachesize int
filesize int
}
func NewCacheFile(filename string, cachesize int) *CacheFile {
rval := &CacheFile{filename: filename, cachesize: cachesize}
return rval
}
func (this *CacheFile) GetFileName() string {
return this.filename
}
func (this *CacheFile) GetFileSize() int {
return this.filesize
}
func (this *CacheFile) Flush() {
if this.fw != nil {
this.fw.Flush()
this.fw = nil
}
}
func (this *CacheFile) Close() error {
if this.fw != nil {
this.fw.Flush()
this.fw = nil
}
if this.fh != nil {
this.fh.Close()
this.fh = nil
}
return nil
}
func (this *CacheFile) CheckOpen() error {
if this.fw != nil {
return nil
}
f1, err := os.OpenFile(this.filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
return err
}
info, _ := os.Stat(this.filename)
this.fh = f1
this.filesize = int(info.Size())
this.fw = bufio.NewWriterSize(f1, this.cachesize)
return nil
}
func (this *CacheFile) WriteString(s string) (int, error) {
return this.Write([]byte(s))
}
func (this *CacheFile) Write(buf []byte) (int, error) {
if this.fw == nil {
err := this.CheckOpen()
if err != nil {
return -1, err
}
}
n, err := this.fw.Write(buf)
if n > 0 {
this.filesize += n
}
return n, err
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ymofen/gobase.git
git@gitee.com:ymofen/gobase.git
ymofen
gobase
gobase
v1.2.24053

搜索帮助

344bd9b3 5694891 D2dac590 5694891