当前位置:网站首页>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()
}
边栏推荐
猜你喜欢
How was MP3 born?
【LeetCode】17、电话号码的字母组合
MP3是如何诞生的?
Redis 排查大 key 的3种方法,优化必备
QT - plot other problems
GTEST from ignorance to proficiency (3) what are test suite and test case
Analysis of maker education technology in the Internet Era
Compréhension approfondie du symbole [langue C]
每日一题-LeetCode556-下一个更大元素III-字符串-双指针-next_permutation
如何借助自动化工具落地DevOps
随机推荐
How to implement Devops with automatic tools
Go语言循环语句(第10课中3)
挖财学院股票开户安全吗?开户只能在挖财开户嘛?
网上开户哪家证券公司佣金最低,我要开户,网上开户安全吗
Billions of citizens' information has been leaked! Is there any "rescue" for data security on the public cloud?
QT—绘制其他问题
解决异步接口慢导致的数据错乱问题
置信区间的画法
Acwing 2022 daily question
每日一题-LeetCode556-下一个更大元素III-字符串-双指针-next_permutation
flink1.13 sql基础语法(一)DDL、DML
Enlightenment of maker thinking in Higher Education
Redis cache
In the release version, the random white screen does not display the content after opening the shutter
Can be displayed in CAD but not displayed in print
GTEST from ignorance to proficient use (2) what is test fixture
AcWing 2022 每日一题
QT—双缓冲绘图
2021 CCPC Harbin B. magical subsequence (thinking question)
MongoDB聚合操作总结