当前位置:网站首页>File read write
File read write
2022-07-04 21:51:00 【Jimmy_ jimi】
Go Linguistic os There is a OpenFile function , The prototype is shown below :
func OpenFile(name string, flag int, perm FileMode) (file *File, err error)
O_RDONLY: Open file in read-only mode ;
O_WRONLY: Write only mode open file ;
O_RDWR: Open file in read-write mode ;
O_APPEND: Attach data to the end of the file when writing ( Additional );
O_CREATE: If it doesn't exist, a new file will be created ;
O_EXCL: and O_CREATE In combination with , The file must not exist , Otherwise an error is returned ;
O_SYNC: When doing a series of write operations , Every time I have to wait for the last time I/O When the operation is finished, proceed ;
O_TRUNC: If possible , Empty file on open .
example
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
// Create a new file , Write content 5 sentence “http://c.biancheng.net/golang/”
filePath := "e:/code/golang.txt"
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0666)
// Open an existing file , Add content to the original content
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
fmt.Println(" File opening failure ", err)
}
// Read the contents of the original file , And it's displayed on the terminal
reader := bufio.NewReader(file)
for {
str, err := reader.ReadString('\n')
if err == io.EOF {
break
}
fmt.Print(str)
}
// Shut down in time file Handle
defer file.Close()
// When writing a file , Use cached *Writer
write := bufio.NewWriter(file)
for i := 0; i < 5; i++ {
write.WriteString("http://c.biancheng.net/golang/ \n")
}
//Flush Write the cached file to the file
write.Flush()
}
边栏推荐
- 类方法和类变量的使用
- Cadeus has never stopped innovating. Decentralized edge rendering technology makes the metauniverse no longer far away
- 巅峰不止,继续奋斗!城链科技数字峰会于重庆隆重举行
- 输入的查询SQL语句,是如何执行的?
- How to use concurrentlinkedqueue as a cache queue
- 哈希表(Hash Tabel)
- 置信区间的画法
- Rotary transformer string judgment
- Maya lamp modeling
- Super detailed tutorial, an introduction to istio Architecture Principle and practical application
猜你喜欢

ArcGIS 10.2.2 | solution to the failure of ArcGIS license server to start

做BI开发,为什么一定要熟悉行业和企业业务?

历史最全混合专家(MOE)模型相关精选论文、系统、应用整理分享

奋斗正当时,城链科技战略峰会广州站圆满召开

OMS系统实战的三两事
![[early knowledge of activities] list of recent activities of livevideostack](/img/14/d2cdae45a18a5bba7ee1ffab903af2.jpg)
[early knowledge of activities] list of recent activities of livevideostack

redis03——Redis的网络配置与心跳机制

一文掌握数仓中auto analyze的使用
![[wechat applet] collaborative work and release](/img/14/2658cf0ba6be9432c74b2490e53d05.png)
[wechat applet] collaborative work and release

TCP三次握手,四次挥手,你真的了解吗?
随机推荐
Jerry added the process of turning off the touch module before turning it off [chapter]
How much is the minimum stock account opening commission? Is it safe to open an account online
numpy vstack 和 column_stack
minidom 模塊寫入和解析 XML
Redis cache
gtest从一无所知到熟练使用(3)什么是test suite和test case
网上开户哪家证券公司佣金最低,我要开户,网上开户安全吗
How is the entered query SQL statement executed?
Sorting and sharing of selected papers, systems and applications related to the most comprehensive mixed expert (MOE) model in history
Interviewer: what is XSS attack?
挖财学院股票开户安全吗?开户只能在挖财开户嘛?
Cadeus has never stopped innovating. Decentralized edge rendering technology makes the metauniverse no longer far away
一文掌握数仓中auto analyze的使用
股票开户流程是什么?使用同花顺手机炒股软件安全吗?
【C语言】符号的深度理解
Billions of citizens' information has been leaked! Is there any "rescue" for data security on the public cloud?
Jerry's ad series MIDI function description [chapter]
Analysis of maker education technology in the Internet Era
从RepVgg到MobileOne,含mobileone的代码
输入的查询SQL语句,是如何执行的?