当前位置:网站首页>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()
}
边栏推荐
- Jerry's ad series MIDI function description [chapter]
- 【C語言】符號的深度理解
- Jerry added the process of turning off the touch module before turning it off [chapter]
- Go language loop statement (3 in Lesson 10)
- SolidWorks工程图添加材料明细表的操作
- Liu Jincheng won the 2022 China e-commerce industry innovation Figure Award
- new IntersectionObserver 使用笔记
- PostgreSQL基本结构——表
- 什么是商业智能(BI),就看这篇文章足够了
- 改善机器视觉系统的方法
猜你喜欢
Case sharing | integrated construction of data operation and maintenance in the financial industry
TCP三次握手,四次挥手,你真的了解吗?
WGCNA analysis basic tutorial summary
Jerry's ad series MIDI function description [chapter]
Application practice | Shuhai supply chain construction of data center based on Apache Doris
Jerry's ad series MIDI function description [chapter]
改善机器视觉系统的方法
迈动互联中标北京人寿保险
巅峰不止,继续奋斗!城链科技数字峰会于重庆隆重举行
QT—绘制其他问题
随机推荐
刘锦程荣获2022年度中国电商行业创新人物奖
Redis03 - network configuration and heartbeat mechanism of redis
Go language loop statement (3 in Lesson 10)
Monitor the shuttle return button
How to remove the black dot in front of the title in word document
A quick start to fastdfs takes you three minutes to upload and download files to the ECS
开户哪家券商比较好?网上开户安全吗
Golang interview finishing three resumes how to write
历史最全混合专家(MOE)模型相关精选论文、系统、应用整理分享
Redis cache
Analysis of maker education technology in the Internet Era
Lambdaquerywrapper usage
Redis pipeline
[optimtool.unconstrained] unconstrained optimization toolbox
旋变串判断
Bookmark
Operation of adding material schedule in SolidWorks drawing
Flink1.13 SQL basic syntax (I) DDL, DML
QT—绘制其他问题
超详细教程,一文入门Istio架构原理及实战应用