Go language file manipulation code summary
- 2020-06-19 10:30:54
- OfStack
# Create and open files
// A new file can be created in the following two ways:
func Create(name string) (file *File, err Error)
Creates a new file based on the file name provided, returns 1 file object with default permissions of 0666, returns file objects that are read-write.
[
func NewFile(fd uintptr, name string) *File
Creates a file based on the file descriptor, returning 1 file object
// Open the file in the following two ways:
func Open(name string) (file *File, err Error)
This method opens a file named name, but it is read-only, and the internal implementation actually calls OpenFile.
[
func OpenFile(name string, flag int, perm uint32) (file *File, err Error)
Open the file named name, flag is the way to open, read only, read and write, perm is the permission
/ / write file
func (file *File) Write(b []byte) (n int, err Error)
Write information of type byte to a file
[
func (file *File) WriteAt(b []byte, off int64) (n int, err Error)
Starts writing byte type information at the specified location
[
func (file *File) WriteString(s string) (ret int, err Error)
Write the string information to a file
/ / read the file
func (file *File) Read(b []byte) (n int, err Error)
Read the data to b
[
func (file *File) ReadAt(b []byte, off int64) (n int, err Error)
Start reading data from off to b
// Delete files
func Remove(name string) Error
Call this function to delete the file named name
//// Close the file
func (file *File)Close()
]
Write files
// code_034_os_write_to_file project main.go
package main
import (
"fmt"
"os"
)
func main() {
// The new file
fout, err := os.Create("./createfile.txt")
if err != nil {
fmt.Println(err)
return
}
defer fout.Close()
for i := 0; i < 5; i++ {
// Remark: windows environment , At the end \r\n To wrap ,linux Under the \n You can
outstr := fmt.Sprintf("%s:%d\r\n", "Hello Go", i) //Sprintf Console output with a return value string
// Written to the file
fout.WriteString(outstr) //string information
fout.Write([]byte("abcd\r\n")) //byte type
}
}
Read the file
// code_035_os_read_from_file project main.go
package main
import (
"fmt"
"os"
)
func main() {
fin, err := os.Open("./open_and_read1.txt")
if err != nil {
fmt.Println(err) // If the file does not exist: The system cannot find the file specified.
}
defer fin.Close()
buf := make([]byte, 1024) // Create a slice
for {
n, _ := fin.Read(buf) // Read the file
if n == 0 {
break
}
fmt.Println(string(buf))
}
}
The terminal switches to the current directory and executes go run ES133en.go ES135en_ES136en_file_file.txt
// code_036_os_copy_file project main.go
package main
import (
"fmt"
"io"
"os"
)
func main() {
// Use the command line to improve copy reuse
args := os.Args
if args == nil || len(args) != 3 {
fmt.Println("useage : go filename.go src File dstFile")
return
}
srcPath := args[1]
dstPath := args[2]
fmt.Printf("srcPath = %s, dstPath = %s\r\n", srcPath, dstPath)
if srcPath == dstPath {
fmt.Println(" Source and destination files cannot have the same name ")
}
// copy
srcFile, err1 := os.Open(srcPath)
if err1 != nil {
fmt.Println(err1)
return
}
dstFile, err2 := os.Create(dstPath)
if err2 != nil {
fmt.Println(err2)
return
}
read_buf := make([]byte, 1024)
for {
// Read the file
n, err := srcFile.Read(read_buf) // The length of bytes per file read
if err != nil && err != io.EOF {
fmt.Println(err)
break
}
if n == 0 {
fmt.Println(" File processing completed ")
break
}
// Write to destination file
write_buf := read_buf[:n]
dstFile.Write(write_buf)
}
// Close the file
srcFile.Close()
dstFile.Close()
}