Write, append, read and copy operations of golang files
There is an OpenFile function under the os package of the Go language. Its prototype is as follows:
func OpenFile(name string, flag int, perm FileMode) (file *File, err error)
Where name is the file name of the file. If you are not running under the current path, you need to add the specific path; flag is a file processing parameter of type int. the specific value may vary depending on the system, but the function is the same.
Some common flag file processing parameters are listed below:
- O_RDONLY: open the file in read-only mode;
- O_WRONLY: open the file in write only mode;
- O_RDWR: open a file in read-write mode;
- O_APPEND: append data to the end of the file during write operation (append);
- O_CREATE: if it does not exist, a new file will be created;
- O_EXCL: and o_ When used with create, the file must not exist, or an error will be returned;
- O_SYNC: when performing a series of write operations, wait for the last I/O operation to complete each time;
- O_TRUNC: if possible, empty the file when opening.
[example 1]: create a new file, golang Txt and write 5 sentences in it“ http://c.biancheng.net/golang/ ”.
package mainimport ( "bufio" "fmt" "os")func main() { //Create a new file and write 5 sentences“ http://c.biancheng.net/golang/ ” filePath := "e:/code/golang.txt" file, err := os. OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0666) if err != Nil {fmt.println ("file open failed", err)} / / close the file handle defer file Close() / / when writing a file, use *writer with cache to 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()}
After successful execution, a golang Txt file, as shown in the following figure:
[example 2]: open an existing file and add the content "C language Chinese website" to the original content
package mainimport ( "bufio" "fmt" "os")func main() { filePath := "e:/code/golang.txt" file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_APPEND, 0666) if err != nil { fmt.Println("File open failed", err) } //Close the file handle defer file Close() / / when writing a file, use *writer with cache to write: = bufio NewWriter(file) for i := 0; i < 5; I++ {write.writestring ("C language Chinese network \r\n")} / / flush writes the cached file to the file write Flush()}
After successful execution, open golang Txt file was successfully appended, as shown in the following figure:
[example 3]: open an existing file, read out the original content, display it on the terminal, and add 5 sentences "Hello, C language Chinese network".
package mainimport ( "bufio" "fmt" "io" "os")func main() { filePath := "e:/code/golang.txt" file, err := os.OpenFile(filePath, os.O_RDWR|os.O_APPEND, 0666) if err != nil { fmt.Println("File open failed", err) } //Close the file handle defer file Close() / / read the contents of the original file and display it on the terminal Reader: = bufio NewReader(file) for { str, err := reader.ReadString('\n') if err == io.EOF { break } fmt. Print (STR)} / / when writing a file, use a cached *writer to write: = bufio NewWriter(file) for i := 0; i < 5; I++ {write.writestring ("Hello, C language Chinese network. \r\n")} / / flush writes the cached file to the file write Flush()}
After successful execution, the contents of the file will be printed on the console and the specified contents will be appended to the file, as shown in the following figure:
[example 4]: write a program to copy the contents of one file to another file (Note: both files already exist)
package mainimport ( "fmt" "io/ioutil")func main() { file1Path := "e:/code/golang.txt" file2Path := "e:/code/other.txt" data, err := ioutil.ReadFile(file1Path) if err != nil { fmt.Printf("File open failed=%v\n", err) return } err = ioutil.WriteFile(file2Path, data, 0666) if err != nil { fmt.Printf("File open failed=%v\n", err) }}
After successful execution, it is found that the content has been copied successfully, as shown in the following figure:
Reference link:
http://c.biancheng.net/view/5729.html