用外链技术做视频网站,wordpress 编辑器增加按钮,濮阳百姓网免费发布信息网,wordpress 36氪前言
通过案例展示如何读取文件里的内容。本文接着上篇文章的内容#xff0c;介绍文件的写入操作。
File.Write、File.WriteString、File.WriteAt
File.Write(b []byte) (n int, err error) 直接操作磁盘往文件里写入数据#xff0c;写入单位为字节。
b 参数#xff1a;…前言
通过案例展示如何读取文件里的内容。本文接着上篇文章的内容介绍文件的写入操作。
File.Write、File.WriteString、File.WriteAt
File.Write(b []byte) (n int, err error) 直接操作磁盘往文件里写入数据写入单位为字节。
b 参数写入的数据类型为字节切片。 返回值 n写入的字节数。 返回值 err写入数据的过程中产生的错误。
File.WriteString(s string) (n int, err error) 直接操作磁盘往指定文件里写入数据写入单位为字符串。
s 参数写入的字符串数据。 返回值 n写入的字节数。 返回值 err写入数据的过程中产生的错误。
File.WriteAt(b []byte, off int64) (n int, err error) 从指定位置 off 往文件里顺序写入数据如果某个偏移量上有数据则会覆盖。
b 参数写入的数据类型为字节切片。 off 参数偏移量从此位置开始写入数据。 返回值 n写入的字节数。 返回值 err写入数据的过程中产生的错误。
文件写入操作
import (fmtos
)func main() {file, err : os.OpenFile(test.txt, os.O_CREATE, 0)if err ! nil {fmt.Println(err)return}defer file.Close()count, err : file.Write([]byte{H, e, l, l, o, , W, o, r, l, d, \n})if err ! nil {return}fmt.Printf(写入了 %d 字节\n, count)count, err file.WriteString(Hello Golang)if err ! nil {return}fmt.Printf(写入了长度为 %d 的字符串\n, count)count, err file.WriteAt([]byte{x, x, x, x, x, x, x, x, x, x, x}, 0)if err ! nil {return}fmt.Printf(写入了 %d 字节\n, count)
}
首先打开 test.txt 文件指定的模式为 os.O_CREATE如果文件不存在则会自动创建 然后通过 Write 方法以字符的形式往文件里写入 Hello World\n 的内容 接着通过 WriteString 方法以字符串的形式往文件里写入 Hello Golang 内容此时文件里的内容如下所示
Hello World
Hello Golang最后通过 WriteAt 方法指定从偏移量为 0 的位置开始写入数据 xxxxxxxxxxx由于 0 以及之后位置都有数据因此原有数据被覆盖了。最后文件的内容为
xxxxxxxxxxx
Hello GolangFile.Seek
File.Seek(offset int64, whence int) 相对于开头位置或当前位置或末尾位置将设置当前读或写的偏移量设置为 offset。
offset 参数所要设置的偏移量。 whence相对于哪个位置开始设置偏移量的标志可选值为 0 → 开头位置1 → 当前位置2 → 末尾位置。
应用
import (fmtos
)func main() {file, err : os.OpenFile(test.txt, os.O_CREATE, 0)if err ! nil {fmt.Println(err)return}defer file.Close()_, err file.WriteString(G0lang)if err ! nil {return}_, err file.Seek(1, 0)if err ! nil {fmt.Println(err)return}_, err file.Write([]byte{o})if err ! nil {fmt.Println(err)return}
}
打开 test.txt 文件指定的模式为 os.O_CREATE如果文件不存在则会自动创建 使用 WriteString 方法往文件里写入 G0lang 字符串 此时发现第二个字符错了0 应该改成 o此时的偏移量是指向尾部的使用 Seek 方法将偏移量移到第二个位置然后写入字符 o由于当前位置已有数据 0因此 o 将会覆盖 0
bufio.NewWriter、Writer.WriteString、Writer.Flush
如果需要多次执行写入文件的操作推荐使用 bufio 里的 Writer 结构体去操作它会开辟一个缓冲区默认大小为 4096 字节。在数据没有被刷入磁盘之前所写入的数据都会暂时保存到缓冲区里。
NewWriter(w io.Writer) *Writer 开辟一个默认值为 4096 字节的缓冲区用于暂存写入文件的数据内容返回一个 Writer 结构体的指针变量
w 参数类型为 Writer 接口实现这个接口的数据类型变量都可以作为参数例如 File。 返回值 *Writer一个 Writer 结构体的指针变量通过该变量可以往缓冲区里写入数据。
Writer.WriteString(s string) (int, error) 往缓冲区写入内容的方法。
参数 s 为写入的字符串。 第一个返回值为写入的字节数。 第二个返回值为写入数据的过程中产生的错误。
Writer.Flush() error 将所有的缓存数据写入磁盘。
返回值为数据写入磁盘的过程中产生的错误。
文件写入操作 import (bufiofmtos
)func main() {file, err : os.OpenFile(test.txt, os.O_CREATE, 0)if err ! nil {fmt.Println(err)return}defer file.Close()writer : bufio.NewWriter(file)_, err writer.WriteString(Hello World\n)if err ! nil {fmt.Println(err)return}_, err writer.WriteString(Hello Golang\n)if err ! nil {fmt.Println(err)return}_, err writer.WriteString(Hello Gopher\n)if err ! nil {fmt.Println(err)return}writer.Flush()
}首先打开 test.txt 文件指定的模式为 os.O_CREATE如果文件不存在则会自动创建 然后使用 NewWriter 函数获取一个 Writer 结构体的指针变量 writer 接着通过 writer 的 WriteString 方法将内容保存到缓冲区里 最后调用 Flush 方法将所有的缓存数据写入磁盘。
小结
本文先是对 File.Write、File.WriteString、File.WriteAt 进行介绍通过例子演示它们的使用方式然后介绍 File.Seek说明了它的用法最后引出 bufio.NewWriter、Writer.WriteString、Writer.Flush使用它们代替 File 结构体里的写入方法可以不用频繁操作磁盘提高写入效率。 文章转载自: http://www.morning.lwqst.cn.gov.cn.lwqst.cn http://www.morning.prmbb.cn.gov.cn.prmbb.cn http://www.morning.czlzn.cn.gov.cn.czlzn.cn http://www.morning.mkpkz.cn.gov.cn.mkpkz.cn http://www.morning.xbwqg.cn.gov.cn.xbwqg.cn http://www.morning.snbrs.cn.gov.cn.snbrs.cn http://www.morning.hlppp.cn.gov.cn.hlppp.cn http://www.morning.xdpjf.cn.gov.cn.xdpjf.cn http://www.morning.twmp.cn.gov.cn.twmp.cn http://www.morning.ubpsa.cn.gov.cn.ubpsa.cn http://www.morning.zsleyuan.cn.gov.cn.zsleyuan.cn http://www.morning.qbwmz.cn.gov.cn.qbwmz.cn http://www.morning.rkjb.cn.gov.cn.rkjb.cn http://www.morning.fdfdz.cn.gov.cn.fdfdz.cn http://www.morning.lqytk.cn.gov.cn.lqytk.cn http://www.morning.xkbdx.cn.gov.cn.xkbdx.cn http://www.morning.fyglg.cn.gov.cn.fyglg.cn http://www.morning.hfyll.cn.gov.cn.hfyll.cn http://www.morning.hkshy.cn.gov.cn.hkshy.cn http://www.morning.kpxnz.cn.gov.cn.kpxnz.cn http://www.morning.rjnm.cn.gov.cn.rjnm.cn http://www.morning.pyncm.cn.gov.cn.pyncm.cn http://www.morning.zlfxp.cn.gov.cn.zlfxp.cn http://www.morning.ggfdq.cn.gov.cn.ggfdq.cn http://www.morning.pxspq.cn.gov.cn.pxspq.cn http://www.morning.xrmwc.cn.gov.cn.xrmwc.cn http://www.morning.lwnb.cn.gov.cn.lwnb.cn http://www.morning.xbptx.cn.gov.cn.xbptx.cn http://www.morning.rhmpk.cn.gov.cn.rhmpk.cn http://www.morning.knwry.cn.gov.cn.knwry.cn http://www.morning.nrqnj.cn.gov.cn.nrqnj.cn http://www.morning.fjntg.cn.gov.cn.fjntg.cn http://www.morning.pycpt.cn.gov.cn.pycpt.cn http://www.morning.xcbnc.cn.gov.cn.xcbnc.cn http://www.morning.zrpys.cn.gov.cn.zrpys.cn http://www.morning.khpx.cn.gov.cn.khpx.cn http://www.morning.gtwtk.cn.gov.cn.gtwtk.cn http://www.morning.spwm.cn.gov.cn.spwm.cn http://www.morning.zglrl.cn.gov.cn.zglrl.cn http://www.morning.dhtdl.cn.gov.cn.dhtdl.cn http://www.morning.crsnb.cn.gov.cn.crsnb.cn http://www.morning.muzishu.com.gov.cn.muzishu.com http://www.morning.dglszn.com.gov.cn.dglszn.com http://www.morning.fwblh.cn.gov.cn.fwblh.cn http://www.morning.tkqzr.cn.gov.cn.tkqzr.cn http://www.morning.mtqqx.cn.gov.cn.mtqqx.cn http://www.morning.mprpx.cn.gov.cn.mprpx.cn http://www.morning.srnhk.cn.gov.cn.srnhk.cn http://www.morning.wklyk.cn.gov.cn.wklyk.cn http://www.morning.zffps.cn.gov.cn.zffps.cn http://www.morning.jcbmm.cn.gov.cn.jcbmm.cn http://www.morning.wnxqf.cn.gov.cn.wnxqf.cn http://www.morning.dbhnx.cn.gov.cn.dbhnx.cn http://www.morning.sltfk.cn.gov.cn.sltfk.cn http://www.morning.kztts.cn.gov.cn.kztts.cn http://www.morning.gchqy.cn.gov.cn.gchqy.cn http://www.morning.ntzfj.cn.gov.cn.ntzfj.cn http://www.morning.bdzps.cn.gov.cn.bdzps.cn http://www.morning.zynjt.cn.gov.cn.zynjt.cn http://www.morning.rdwm.cn.gov.cn.rdwm.cn http://www.morning.rnribht.cn.gov.cn.rnribht.cn http://www.morning.nccyc.cn.gov.cn.nccyc.cn http://www.morning.yfphk.cn.gov.cn.yfphk.cn http://www.morning.lwygd.cn.gov.cn.lwygd.cn http://www.morning.kbqws.cn.gov.cn.kbqws.cn http://www.morning.rgnq.cn.gov.cn.rgnq.cn http://www.morning.kynf.cn.gov.cn.kynf.cn http://www.morning.woyoua.com.gov.cn.woyoua.com http://www.morning.jkrrg.cn.gov.cn.jkrrg.cn http://www.morning.lzrpy.cn.gov.cn.lzrpy.cn http://www.morning.bfsqz.cn.gov.cn.bfsqz.cn http://www.morning.qwyms.cn.gov.cn.qwyms.cn http://www.morning.ptqbt.cn.gov.cn.ptqbt.cn http://www.morning.qcztm.cn.gov.cn.qcztm.cn http://www.morning.wrkhf.cn.gov.cn.wrkhf.cn http://www.morning.mntxalcb.com.gov.cn.mntxalcb.com http://www.morning.nyjgm.cn.gov.cn.nyjgm.cn http://www.morning.bxbnf.cn.gov.cn.bxbnf.cn http://www.morning.ndzhl.cn.gov.cn.ndzhl.cn http://www.morning.trjr.cn.gov.cn.trjr.cn