This is a creation in Article, where the information may have evolved or changed.
1. Introduction to related APIs
1.1 Creating the File memory address API
//返回File的内存地址,错误信息,通过os库调用funcstring) (file *File, err Error)//返回文件的内存地址,通过os库调用funcintstring) *File
1.2 Open File API
//返回File的内存地址,错误信息,通过os库调用funcstring)(file *File,err Error)//返回File的内存地址,错误信息,通过os库调用funcstringint,perm unit32)(file *File,err Error)
1.3 Write File API
//写入一个slice,返回写的个数,错误信息,通过File的内存地址调用func (file *File)Write(b []byteint,err Error)//从slice的某个位置开始写入,返回写的个数,错误信息,通过File的内存地址调用func (file *File)WriteAt(b []byteint64int,err Error)//写入一个字符串,返回写的个数,错误信息,通过File的内存地址调用funcstringint,err Error)
1.4 Read File API
//读取一个slice,返回读的个数,错误信息,通过File的内存地址调用func (file *File) Read(b []byteint, err Error)//从slice的某个位置开始读取,返回读到的个数,错误信息,通过File的内存地址调用func (file *File) ReadAt(b []byteint64int,err Error)
1.5 Delete File API
//传入文件的路径来删除文件,返回错误个数funcstring) Error
2. Write the instance code of the file
PackageMainImport("FMT" "OS")funcMain () {userfile: ="D:/test.txt" //file pathFout,err: = os. Create (UserFile)//Create a file memory address according to the path deferFout. Close ()//Delay Closing Resources ifErr! =Nil{FMT. Println (Userfile,err)return}//Loop write data to file forI=0; I<10; i++{Fout. WriteString ("Hello world!\r\n")//write StringFout. Write ([]byte("abcd!\r\n"))//strong turn into byte slice and write again}}
Output file Contents:
Hello world!abcd!Hello world!abcd!Hello world!abcd!Hello world!abcd!Hello world!abcd!Hello world!abcd!Hello world!abcd!Hello world!abcd!Hello world!abcd!Hello world!abcd!
3. Read the file instance code
PackageMainImport("FMT" "OS")funcMain () {userfile: ="D:/test.txt" //file pathFin,err: = os. Open (UserFile)//Open file, return the memory address deferFin. Close ()//Delay Closing Resources ifErr! =Nil{FMT. Println (Userfile,err)return} BUF: = Make([]byte, 1024x768)//Create a slice with an initial capacity of 1024 as a buffer container for{//loop to read the file data into the buffer container and return the number of readsN,_: = Fin. Read (BUF)if0==n{ Break //If the number of reads is 0, the reading is complete and the loop jumps out}//write the data from the buffer slice, from the slice subscript 0 to N, through the OS. StdOut Write to consoleOs. Stdout.write (Buf[:n])}}
Operation Result:
G:\gocodehome\demo\bin>go Build Demo4
G:\GoCodeHome\demo\bin>demo4Hello world!abcd!Hello world!abcd!Hello world!abcd!Hello world!abcd!Hello world!abcd!Hello world!abcd!Hello world!abcd!Hello world!abcd!Hello world!abcd!Hello world!abcd!
4. Use the OS at the same time. Open and os.create operation files
PackageMainImport("IO" "OS")funcMain () {fi, err: = OS. Open ("D:/input.txt")//Open input *file ifErr! =Nil{Panic(ERR)}deferFi. Close () FO, err: = OS. Create ("D:/output.txt")//Create output *file ifErr! =Nil{Panic(ERR)}deferFo. Close () BUF: = Make([]byte,1024x768) for{N, err: = fi. Read (BUF)//read from Input.txt ifErr! =Nil&& Err! = Io. EOF {Panic(ERR)}ifn = =0{ Break}ifN2, err: = fo. Write (Buf[:n]); Err! =Nil{//write output.txt until error Panic(ERR)}Else ifN2! = n {Panic("Error in writing") } }}
5. Using the Bufio Library
PackageMainImport("Bufio" "IO" "OS")funcMain () {fi, err: = OS. Open ("Input.txt")//Open input *file ifErr! =Nil{Panic(ERR)}deferFi. Close () r: = Bufio. Newreader (FI)//Create a read buffer streamFO, err: = OS. Create ("Output.txt")//Create output *file ifErr! =Nil{Panic(ERR)}deferFo. Close () W: = Bufio. Newwriter (FO)//Create output Buffer streamBUF: = Make([]byte,1024x768) for{N, err: = R.read (BUF)ifErr! =Nil&& Err! = Io. EOF {Panic(ERR)}ifn = =0{ Break}ifN2, Err: = W.write (Buf[:n]); Err! =Nil{Panic(ERR)}Else ifN2! = n {Panic("Error in writing") } }ifErr = W.flush (); Err! =Nil{Panic(ERR)}}
6. Using the Ioutil Library
package mainimport ( "io/ioutil")func main() { b, err := ioutil.ReadFile("input.txt")//读文件 ifnilpanic(err) } err = ioutil.WriteFile("output.txt", b, 0644)//写文件 ifnilpanic(err) }}
7. Traverse a Folder
PackageMainImport("Path/filepath" "OS" "FMT" "Flag")funcGetfilelist (Pathstring{err: = filepath. Walk (Path,func(Pathstring, F OS. FileInfo, err Error) error {if(f = =Nil) {returnErrifF.isdir () {return Nil}println(path)return Nil})ifErr! =Nil{FMT. Printf ("filepath. Walk () returned%v\n ", err)}}funcMain () {flag. Parse () Root: = flag. Arg(0) Getfilelist (Root)}
Operation Result: