当前位置:网站首页>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()
}
边栏推荐
- 开源之夏专访|Apache IoTDB社区 新晋Committer谢其骏
- Keep on fighting! The city chain technology digital summit was grandly held in Chongqing
- 解析互联网时代的创客教育技术
- Day24: file system
- QT—双缓冲绘图
- [wechat applet] collaborative work and release
- Delphi soap WebService server-side multiple soapdatamodules implement the same interface method, interface inheritance
- redis03——Redis的网络配置与心跳机制
- GTEST from ignorance to proficient use (2) what is test fixture
- Caduceus从未停止创新,去中心化边缘渲染技术让元宇宙不再遥远
猜你喜欢
巅峰不止,继续奋斗!城链科技数字峰会于重庆隆重举行
QT - double buffer plot
Case sharing | integrated construction of data operation and maintenance in the financial industry
LambdaQueryWrapper用法
[leetcode] 17. Letter combination of telephone number
Operation of adding material schedule in SolidWorks drawing
bizchart+slider实现分组柱状图
redis03——Redis的网络配置与心跳机制
Bookmark
Redis has three methods for checking big keys, which are necessary for optimization
随机推荐
Flutter 返回按钮的监听
开源之夏专访|Apache IoTDB社区 新晋Committer谢其骏
Shutter WebView example
改善机器视觉系统的方法
Redis 排查大 key 的3种方法,优化必备
GTEST from ignorance to skillful use (1) GTEST installation
迈动互联中标北京人寿保险
Golang面试整理 三 简历如何书写
Flink1.13 SQL basic syntax (I) DDL, DML
股票开户流程是什么?使用同花顺手机炒股软件安全吗?
How to use concurrentlinkedqueue as a cache queue
gtest从一无所知到熟练使用(4)如何用gtest写单元测试
Daily question -leetcode1200- minimum absolute difference - array - sort
做BI开发,为什么一定要熟悉行业和企业业务?
Billions of citizens' information has been leaked! Is there any "rescue" for data security on the public cloud?
输入的查询SQL语句,是如何执行的?
HDU - 1078 FatMouse and Cheese(记忆化搜索DP)
Can be displayed in CAD but not displayed in print
Why does invariant mode improve performance
Open3D 曲面法向量计算