Introduction to the File Operations API
New File
Func Create (name string) (file *file, err Error)
Creates a new file based on the file name provided, returns a file object with the default permission of 0666, and the returned file object is writable.
Func NewFile (fd uintptr, name string) *file
Creates the appropriate file based on the file descriptor, returns a file object to open the file
Func Open (name string) (file *file, err Error)
The method opens a file named name, but is read-only, and the internal implementation actually invokes OpenFile.
Func OpenFile (name string, flag int, perm uint32) (file *file, err Error)
Open the file named Name, flag is open way, read only, read and write, perm is permission to write files
Func (file *file) Write (b []byte) (n int, err Error)
Writes byte-type information to a file
Func (file *file) writeat (b []byte, off Int64) (n int, err Error)
Writes byte-type information at a specified location
Func (file *file) writestring (S string) (ret int, err Error)
Write string information to file read file
Func (file *file) Read (b []byte) (n int, err Error)
reading data to B
Func (file *file) readat (b []byte, off Int64) (n int, err Error)
Read data from off to delete files in B
Func Remove (name string) Error
Call this function to delete file code implementation with filename name
Package main
Import (
"FMT"
"io" "
os"
)
func main () {
list: = OS. Args
If Len (list)!= 3 {
FMT. Println ("Error: Missing parameters")
return
}
srcname: = list[1]
dstname: = list[2]
srcfile, err1: = OS. Open (SrcName)
dstfile, ERR2: = OS. Create (dstname)
if err1!= nil | | err2!= NIL {fmt
. Println ("err")
return
}
defer srcfile.close ()
defer dstfile.close ()
buf: = Make ([]byte, 4* 1024)//4k buffer
//core processing for
{
N, err: = Srcfile.read (BUF)
If err!= nil {
if err = = Io. EOF {//file read complete
break
}
FMT. Println ("ERR:", err)
}
dstfile.write (Buf[:n])//Read how many to write}
}
Run Detection