当前位置:网站首页>文件读取写入
文件读取写入
2022-07-04 20:59:00 【Jimmy_jimi】
Go语言的 os 包下有一个 OpenFile 函数,其原型如下所示:
func OpenFile(name string, flag int, perm FileMode) (file *File, err error)
O_RDONLY:只读模式打开文件;
O_WRONLY:只写模式打开文件;
O_RDWR:读写模式打开文件;
O_APPEND:写操作时将数据附加到文件尾部(追加);
O_CREATE:如果不存在将创建一个新文件;
O_EXCL:和 O_CREATE 配合使用,文件必须不存在,否则返回一个错误;
O_SYNC:当进行一系列写操作时,每次都要等待上次的 I/O 操作完成再进行;
O_TRUNC:如果可能,在打开时清空文件。
实例
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
//创建一个新文件,写入内容 5 句 “http://c.biancheng.net/golang/”
filePath := "e:/code/golang.txt"
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0666)
//打开一个存在的文件,在原来的内容追加内容
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
fmt.Println("文件打开失败", err)
}
//读原来文件的内容,并且显示在终端
reader := bufio.NewReader(file)
for {
str, err := reader.ReadString('\n')
if err == io.EOF {
break
}
fmt.Print(str)
}
//及时关闭file句柄
defer file.Close()
//写入文件时,使用带缓存的 *Writer
write := bufio.NewWriter(file)
for i := 0; i < 5; i++ {
write.WriteString("http://c.biancheng.net/golang/ \n")
}
//Flush将缓存的文件真正写入到文件中
write.Flush()
}
边栏推荐
- MP3是如何诞生的?
- 每日一题-LeetCode556-下一个更大元素III-字符串-双指针-next_permutation
- IIC (STM32)
- Cloudcompare & open3d DBSCAN clustering (non plug-in)
- Compréhension approfondie du symbole [langue C]
- Maidong Internet won the bid of Beijing life insurance
- redis03——Redis的网络配置与心跳机制
- 2021 CCPC Harbin I. power and zero (binary + thinking)
- 学习突围3 - 关于精力
- Jerry added the process of turning off the touch module before turning it off [chapter]
猜你喜欢

开源之夏专访|Apache IoTDB社区 新晋Committer谢其骏

Huawei ENSP simulator configures ACL access control list

迈动互联中标北京人寿保险

【活动早知道】LiveVideoStack近期活动一览

QT—绘制其他问题

MP3是如何诞生的?

Stealing others' vulnerability reports and selling them into sidelines, and the vulnerability reward platform gives rise to "insiders"
![[optimtool.unconstrained] unconstrained optimization toolbox](/img/ef/65379499df205c068ee9bc9df797ac.png)
[optimtool.unconstrained] unconstrained optimization toolbox

超详细教程,一文入门Istio架构原理及实战应用
![[early knowledge of activities] list of recent activities of livevideostack](/img/14/d2cdae45a18a5bba7ee1ffab903af2.jpg)
[early knowledge of activities] list of recent activities of livevideostack
随机推荐
Rotary transformer string judgment
【C语言】符号的深度理解
如何借助自动化工具落地DevOps
[early knowledge of activities] list of recent activities of livevideostack
torch. Tensor and torch The difference between tensor
Case sharing | integrated construction of data operation and maintenance in the financial industry
Flutter 返回按钮的监听
解析互联网时代的创客教育技术
每日一题-LeetCode556-下一个更大元素III-字符串-双指针-next_permutation
Jerry's ad series MIDI function description [chapter]
奋斗正当时,城链科技战略峰会广州站圆满召开
输入的查询SQL语句,是如何执行的?
Hash table
开源之夏专访|Apache IoTDB社区 新晋Committer谢其骏
ArcGIS 10.2.2 | solution to the failure of ArcGIS license server to start
Kubeadm初始化报错:[ERROR CRI]: container runtime is not running
redis缓存
Jerry's ad series MIDI function description [chapter]
AcWing 2022 每日一题
GTEST from ignorance to proficiency (3) what are test suite and test case