当前位置:网站首页>文件读取写入
文件读取写入
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()
}
边栏推荐
- 旋变串判断
- 解读创客教育中的各类智能化组织发展
- redis缓存
- [early knowledge of activities] list of recent activities of livevideostack
- Jerry's ad series MIDI function description [chapter]
- Redis pipeline
- 更强的 JsonPath 兼容性及性能测试之2022版(Snack3,Fastjson2,jayway.jsonpath)
- Cadeus has never stopped innovating. Decentralized edge rendering technology makes the metauniverse no longer far away
- Jerry's ad series MIDI function description [chapter]
- GTEST from ignorance to proficiency (3) what are test suite and test case
猜你喜欢

Sorting and sharing of selected papers, systems and applications related to the most comprehensive mixed expert (MOE) model in history

Shutter textfield example

超详细教程,一文入门Istio架构原理及实战应用

Lambdaquerywrapper usage

How is the entered query SQL statement executed?
![[leetcode] 17. Letter combination of telephone number](/img/be/7f456c092f7cda5ebabc2f1cce292e.png)
[leetcode] 17. Letter combination of telephone number

TCP shakes hands three times and waves four times. Do you really understand?
![Jerry's ad series MIDI function description [chapter]](/img/28/e0f9b41db597ff3288af431c677001.png)
Jerry's ad series MIDI function description [chapter]

巅峰不止,继续奋斗!城链科技数字峰会于重庆隆重举行

Analyzing the maker space contained in steam Education
随机推荐
Why does invariant mode improve performance
minidom 模块写入和解析 XML
How to remove the black dot in front of the title in word document
Maya lamp modeling
Daily question -leetcode1200- minimum absolute difference - array - sort
【微信小程序】协同工作与发布
EhLib 数据库记录的下拉选择
How much is the minimum stock account opening commission? Is it safe to open an account online
Jerry's ad series MIDI function description [chapter]
ArcGIS 10.2.2 | solution to the failure of ArcGIS license server to start
Delphi soap WebService server-side multiple soapdatamodules implement the same interface method, interface inheritance
Use of class methods and class variables
Application practice | Shuhai supply chain construction of data center based on Apache Doris
解读创客教育中的各类智能化组织发展
【C语言】符号的深度理解
Liu Jincheng won the 2022 China e-commerce industry innovation Figure Award
Learning breakout 3 - about energy
股票开户佣金最低多少,炒股开户佣金最低网上开户安全吗
【LeetCode】17、电话号码的字母组合
Acwing 2022 daily question