Go file creation and file writing with buffers

os package - os - pkg.go.devhttps://pkg.go.dev/os#OpenFile

os.OpenFile


When writing a file, use a function OpenFile, which has three parameters. This function is a function in the os package.

func OpenFile(name string, flag int, perm FileMode) (*File, error)

flag

Description: os.OpenFile is a more general file open function, it will use the specified options (such as O_RDONLY, etc.), the specified mode (such as 0666, etc.) to open the file with the specified name. If the operation is successful, the returned file object can be used for I/O. If there is an error, the underlying type of the error is *PathError.

The first parameter is the path of the file, and the second parameter is to control the behavior of opening the file, such as whether to open it for reading or writing, whether to append or clear it, and when the file does not exist. Whether to create. The last one is file permissions.

The following are the identifiers of open files: these modes can be combined by |. In file operations, there is no special function to create files. When you open a file, it is opened in O_CREAT mode. If the file is opened If it does not exist, a new file will be created automatically, and we often use O_CREAT in combination with the writing method.

Mode opening is very important. For example, syscall.O_TRUNC, if it is opened incorrectly, the problem is very serious. For example, if it is opened in this way, the original file will be emptied, so when opening it, it must be opened as required. 

const (
	// Exactly one of O_RDONLY, O_WRONLY, or O_RDWR must be specified.
	O_RDONLY int = syscall.O_RDONLY //  只读模式打开文件
	O_WRONLY int = syscall.O_WRONLY //  只写模式打开文件
	O_RDWR   int = syscall.O_RDWR   //  读写模式打开文件

	O_APPEND int = syscall.O_APPEND //  写操作将文件数据追加到文件尾部
	O_CREATE int = syscall.O_CREAT  //  如果不存在将创建一个新文件
	O_EXCL   int = syscall.O_EXCL   // used with O_CREATE, file must not exist.
	O_SYNC   int = syscall.O_SYNC   // open for synchronous I/O.  同步io
	O_TRUNC  int = syscall.O_TRUNC  // 打开截断文件,也就是清空文件
)


默认情况下是异步io

FileMode 

This is mainly used on Linux, and is invalid under Windows operating system.

*File, error

 It is returned as a file handle, or an error value if it is opened incorrectly.

 

 

write file with buffer


Use os.OpenFile(), bufio.NewWriter(), *Writer method WriteString to complete the above task.

There is a cache when operating files, instead of reading the files on the disk every time the system calls. If there is data in the cache, read the data directly from the cache. If there is no data in the cache, it will be loaded from the disk to the cache, and then read.

If it is writing, first write the data to the cache, execute sync when the cache is full, and output the data in the cache to the disk.

func NewWriter 

We use a buffered writer here, and we used func (b * Reader ) ReadString(delim byte ) ( string , error ) when reading before, and also provides a buffered writer, so that we can pass the cache way to write content to the file.

Because it is cached, the written content has not been placed on the disk, and a flush function can be used to actually write the content in the cache to the disk. So when calling func (b * Writer ) WriteString(s string ) ( int , error ), the content is actually written to the cache first.

It can be seen that there is indeed a buffer in the structure. When writing, the content is written to the buffer first, so there must be an action flush to write the content of the buffer to the file.

type Writer struct {
	err error
	buf []byte
	n   int
	wr  io.Writer
}

fileSd,err := os.OpenFile("test.txt",os.O_CREATE | os.O_WRONLY,os.ModePerm)

	if err != nil {
		fmt.Println("open file error:",err)
		return
	}

	defer fileSd.Close()

	contents := []string{"hello\n","world\n","你好\n","世界\n"}
	writer := bufio.NewWriter(fileSd)

	for _,v := range contents{
		writer.WriteString(v)
	}

	writer.Flush() //内容是先写到缓存对,所以需要调用flush将缓存对数据真正写到文件中
                   //否则文件当中会没有数据

Guess you like

Origin blog.csdn.net/qq_34556414/article/details/123517517