
golang bufio浅析
首先,打开目标文件文件。 初始化一个大小为100字节的缓冲区读取器。 创建一个临时缓冲区来存储读取的数据。 读取缓冲区中的数据,并将其写入临时缓冲区。 打印当前缓冲区的大小和已读取数据。 再次读取缓冲区中的数据,更新已读取内容并打印。 打开目标文件文件,设置读写权限。 创建一个大小为10字节的缓冲区写入器。 准备要写入的文本内容。 将文本内容直接写入缓冲区。 读取当前缓冲区的内容并打印。 检查缓冲区是否已满,如果是,则将缓冲区内容写入文件并清空缓冲区。 再次读取文件内容,验证缓冲区是否已正确写入。 打开目标文件文件。 创建一个新的文件扫描器。 读取并打印文件的第一行内容。 循环读取并打印剩余的每一行内容。
发布日期:2021-05-10 23:13:03
浏览次数:17
分类:精选文章
本文共 2185 字,大约阅读时间需要 7 分钟。
读取操作
bufio在读取文件时,会将要读取或缓冲的内容存储在内部缓冲区中。以下是详细的读取过程:
func bufIoRead() { file := "./test-file" fd, err := os.Open(file) if err != nil { panic(err) } defer fd.Close() b := bufio.NewReaderSize(fd, 100) ctx := make([]byte, 30) var buf bytes.Buffer fmt.Println("b size is: ", b.Size()) if _, err := b.Read(ctx); err != nil { panic(err) } buf.Write(ctx) fmt.Println(buf.String()) fmt.Println("剩余缓冲大小: ", b.Buffered()) _, err = b.Read(ctx) if err != nil { panic(err) } buf.Write(ctx) fmt.Println(buf.String()) fmt.Println("剩余缓冲大小: ", b.Buffered()) }
在这个函数中:
写入操作
bufio在写入文件时,也提供了缓存机制。以下是详细的写入过程:
func bufIoWrite() { file := "./test-file" fd, err := os.OpenFile(file, os.O_RDWR, os.ModeAppend) if err != nil { panic(err) } defer fd.Close() cacheSize := 10 b := bufio.NewWriterSize(fd, cacheSize) txt := []byte("world ") if _, err := b.Write(txt); err != nil { panic(err) } cxt := make([]byte, 100) if _, err := fd.Read(cxt); err != nil { if err == io.EOF { fmt.Println("file is empty.") } else { panic(err) } } fmt.Println("before flush cache: ", string(cxt)) fmt.Println("number of bytes that have been written into cache: ", b.Buffered()) fmt.Println("how many bytes are unused in the cache: ", b.Available()) if err := b.Flush(); err != nil { panic(err) } fmt.Println("after flush, number of bytes that have been written into cache: ", b.Buffered()) fmt.Println("after flush, number of bytes are unused in the cache: ", b.Available()) if _, err := fd.ReadAt(cxt, 0); err != nil { if err == io.EOF { fmt.Println("file is empty.") } else { panic(err) } } fmt.Println("after flush cache: ", string(cxt)) }
在这个函数中:
文件扫描操作
bufio还可以用于文件扫描,即按行读取文件内容。以下是详细的扫描过程:
func bufIoScanner() { f, err := os.Open("./test-file") if err != nil { panic(err) } scanner := bufio.NewScanner(f) scanner.Scan() fmt.Println(scanner.Text()) for scanner.Scan() { line := scanner.Text() fmt.Println(line) } }
在这个函数中:
发表评论
最新留言
网站不错 人气很旺了 加油
[***.192.178.218]2025年04月13日 11时20分19秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Linux操作系统的安装与使用
2019-03-12
C++ 继承 详解
2019-03-12
OSPF多区域
2019-03-12
Docker入门之-镜像(二)
2019-03-12
去了解拉绳位移编码器的影响因素
2019-03-12
无法初始化Winsock2.2处理
2019-03-12
vMotion 操作失败进度卡在14% ,报错: Operation Timed out
2019-03-12
重置UAG Application admin密码
2019-03-12
Horizon Daas租户管理平台扩展分配时报:内部错误
2019-03-12
嵌入式系统试题库(CSU)
2019-03-12
【自考】之信息资源管理(一)
2019-03-12
setup facatory9.0打包详细教程(含静默安装和卸载)
2019-03-12
Linux kernel pwn --- CSAW2015 StringIPC
2019-03-12
IDEA 找不到 Persistence窗口解决办法
2019-03-12
C++ Primer Plus读书笔记:循环读取(错误处理)
2019-03-12
Form窗体属性
2019-03-12
vue 错误收集
2019-03-12
00010.02最基础客户信息管理软件(意义类的小项目,练习基础,不涉及数据库)
2019-03-12
00013.05 字符串比较
2019-03-12