go implementation file creation deletion and reading sample code
- 2020-06-19 10:33:21
- OfStack
File directory creation and deletion
package main
import(
"fmt"
"os"
)
func main(){
// Create directories and permissions
os.Mkdir("./benben",0777)
// Create multiple levels of directories and set permissions
os.MkdirAll("./benben/test",0777)
// Delete the directory
err:=os.Remove("./benben")
if err!=nil{
fmt.Println(err)
}
// Delete multilevel directories
os.RemoveAll("./benben")
}
File creation
package main
import (
"os"
"fmt"
)
func main(){
// The creation of the file, Create A file is created based on the file name passed in. The default permission is 0666
file,err:=os.Create("a.txt")
if err!=nil{
fmt.Println(err)
}
defer file.Close()
}
Open and close files
The open and close functions of a file are illustrated in the read and write examples of a file.
Read and write files
There are several ways to read a file:
Method 1: Use os. Open() and ioutil. ReadAll()
package main
func main(){
// Open the file
file,err:=os.Open("a.txt")
if err !=nil {
fmt.Println(err)
}
// File closure
defer file.Close()
// File reading mode 1 Through the os.Open return 1 Handle to a file, and then use it to read
body,err:=ioutil.ReadAll(file)
if err !=nil {
fmt.Println(err)
}
fmt.Println(string(body))
}
Method 2: The ioutil.ReadFile () method is used to implement the read operation.
package main
func main(){
body,err:=ioutil.ReadFile("a.txt")
if err!=nil {
fmt.Println(err)
}
fmt.Println(string(body))
}
The ReadFile function directly divides the three steps in Mode 1 into one and the following is the implementation code.
func ReadFile(filename string)([]byte,error){
// Open the file
f,err:=os.Open(filename)
if err!=nil {
return nil,err
}
// Delayed close file
defer f.Close()
// Sets the number of bytes to read the file
var n int64
//Stat() Method returns an information structure that describes the specified file FileInfo
if fi,err:=f.Stat();err==nil{
if size:=fi.Size();size<1e9{
n=size
}
}
// Returns the contents of the read file
return readAll(f,n+bytes.MinRead)
}
FileInfo has the following methods:
Name() string // Return file name
Size() int64 // Returns the length of bytes of the file
Mode() FileMode // File mode bit
ModTime() time.Time // Modify the time
IsDir() bool // Directory or not
Sys() interface{} // Underlying data source
Method 3: Read with cache
package main
func main(){
file,err:=os.Open("a.txt")
if err!=nil {
fmt.Println(err)
}
defer file.Close()
// create 1 A new one io.Reader It does Read methods
reader:=bufio.NewReader(file)
// Sets the length of the read
buf:=make([]byte,1024)
// Read the file
_,err=reader.Read(buf)
if err!=nil {
fmt.Println(err)
}
fmt.Println(string(buf))
}
File write
Method 1: Use ES46en. Write File()
package main
import (
"io/ioutil"
)
func main(){
content:=[]byte("Go is an open source programming language that makes is easy to build simple,reliable,and efficient software)
err:=ioutil.WriteFile("a.txt",content,0777)
if err!=nil {
fmt.Println(err)
}
fmt.Println("write file successful")
}
Take a look at how WriteFile implements file writing in detail.
func WriteFile(filename string,data []byte,perm os.FileMode) error{
// Open the file
f,err:=os.OpenFile(filename,os.O_WRONLY|os.O_CREATE|os.O_TRUNC,perm)
if err!=nil {
return err
}
// File write
n,err:=f.Write(data)
if err==nil && n<len(data){
err=io.ErrShortWrite
}
// Close the file
if err1:=f.Close();err==nil{
err=err1
}
return err
}
Method 2: Open the file and read and write the file using Write(). Note: instead of using ES57en.Open (), use os.OpenFile (). os. Open() and os. OpenFile()
package main
func main(){
file,err:=os.OpenFile("a.txt",os.O_RDWR|os.O_CREATE,0777)
if err!=nil {
fmt.Println(err)
}
defer file.Close()
content:=[]byte("Go is an open source programing language that makes it easy to build simple,reliable,and efficient software")
_,err=file.Write(content)
if err!=nil {
fmt.Println(err)
}
fmt.Println("write file successful")
}
Method 3: Operate through a package provided by bufio with buffered operations
If you use the Write method for the write operation, you will also need to use the Flush() method at this point. Analysis of Write method in golang bufio package
package main
import (
"os"
"fmt"
)
func main(){
// The creation of the file, Create A file is created based on the file name passed in. The default permission is 0666
file,err:=os.Create("a.txt")
if err!=nil{
fmt.Println(err)
}
defer file.Close()
}
0
conclusion