Related actions for summary files

Source: Internet
Author: User
* * We in the file related operations, often do not understand in which case the use of those functions, each check Baidu lead to inefficient development, I here to enumerate some convenient later query: * * * * * * * * * * * * * * * * * * * * * from a file location, read, write n characters * * * Copy a file to another file, or copy the first n characters of a file to another file * * * * * Aggregation of multiple files into a single file * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * , last modified time, absolute path, rename operation, existence) * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * to write the contents of a file to another file, you can control write content ****1, read the entire file * * ' Gofunc test1 Open ("D:/a.txt") if err! = Nil {panic (err)}defer f.close ()//Read entire file 1. Use Stat to get the file length, then open up space, write the data into the variable, output to the terminal fi, err: = F.stat () if err! = Nil {panic (err)}b: = Make ([]byte, FI. Size ()) If _, err = F.read (b); Err! = Nil {panic (err)}os. Stdout.write (b)//Read entire file 2. Directly using Ioutil's ReadAll function B, err = Ioutil. ReadAll (f) if err! = Nil {panic (err)}os. Stdout.write (b)} ' **2, starting from a location in the file, reading and writing n characters operation * * ' Gofunc test2 () {f, err: = OS. OpenFile ("D:/a.txt", OS. O_RDWR, 0755) if err! = Nil {panic (err)}defer f.close ()//sets the current position of the action file to the 10th character if _, err: = F.seek ( -10, Io. Seekend); Err! = Nil {panic (ERR)}//reads 5 characters from the current position B: = make ([]byte, Ten) If _, Err: = F.readat (b, 5); Err! = Nil {panic (err)}os. StdoUt. Write (b)//writes 5 characters from the current position (equivalent to overwriting the last 5 characters of a file) F. Writeat (b, 5)} "**3, copy the file to another file, or copy the first n characters of the file to another file * * *" "Gofunc test3 () {f1, err: = OS. Open ("D:/a.txt") if err! = Nil {panic (err)}defer F1. Close () F2, err: = OS. OpenFile ("D:/b.txt", OS. O_create | Os. O_RDWR, 0755) if err! = Nil {panic (err)}defer F2. Close ()//Copy all characters of F1 to the front of F2, if the size of F2 is larger than F1, then the following characters remain//if you want to append in F2, use the OS. O_appendif _, Err: = Io. Copy (F2, F1); Err! = Nil {panic (err)}//copies the n characters of F1 to the front of the F2 if _, err: = Io. Copyn (F2, F1, 10); Err! = Nil {panic (err)}} ' **4, aggregating multiple files into one file for operation * * ' Gofunc test4 () {f1, err: = OS. Open ("D:/a.txt") if err! = Nil {panic (err)}defer F1. Close () F2, err: = OS. Open ("D:/b.txt") if err! = Nil {panic (err)}defer F2. Close ()//will be 2 file. Reade merged into a readerr: = Io. Multireader (f1, F2)//poll read data in reader buf: = make ([]byte] Data: = Make ([]byte, 0, +) for n, err: = R.read (BUF); Err! = Io. EOF; N, err = R.read (buf) {if err! = Nil {panic (err)}data = append (data, Buf[:n] ...)} Os. Stdout.write (data)} ' **5, writes multiple characters simultaneously to multiple files * * ' Gofunc test5 () {f, err: = Os. OpenFile ("D:/a.txt", OS. O_create | Os. O_rdwr | Os. O_append, 0755) if err! = Nil {panic (err)}defer f.close ()//combines file and standard output into a writer w: = io. Multiwriter (f, OS. Stdout)//write every write to W contained in all writers. Write ([]byte ("HelloWorld")) W.write ([]byte ("Nihaoshijie")} "**6, get information about the file (name, size, whether it is a folder, file permissions, creation, last access, last modified time, Absolute path, rename operation, existence) * * ' Gofunc Test6 () {f, err: = OS. OpenFile ("D:/a.txt", OS. O_create|os. O_RDWR, 0755) if err! = Nil {panic (err)}defer f.close () fi, err: = F.stat () if err! = Nil {panic (err)}//name, size, whether folder, details, file permissions, last modification time FMT. Printf ("Name:%s\n", fi. Name ()) fmt. Printf ("Size:%d\n", fi. Size ()) fmt. Printf ("Isdir:%t\n", fi. Isdir ()) fmt. Printf ("SYS:%v\n", fi. Sys ()) fmt. Printf ("mode:%o\n", fi.) Mode ()) fmt. Printf ("Modtime:%s\n", fi. Modtime ())//Get details of the file creation time, detailed information see stat_t structure (different Windows and Linux)//windows//createtiontimeuint32// Lastaccesstimeuint32if reflect. ValueOf (FI. Sys ()). Elem (). Fieldbyname ("Createtiontime"). IsValid () {ctim: = reflect. ValueOf (FI. Sys ()). Elem (). Fieldbyname ("Createtiontime"). Field (0).Uint () fmt. Printf ("Ctim:%d\n", Ctim)}//gets the absolute path to the file dir, err: = FilePath. Abs ("D:/a.txt") if err! = Nil {panic (err)}fmt. Printf ("dir:%s\n", dir)//Determine if the file does not exist if _, err: = OS. Stat ("D:/c.txt"); Err! = Nil {if OS. Isnotexist (Err) {FMT. PRINTLN ("File not Exist")} else {panic (err)}}//rename file or folder (to have permission/file closed) If err: = OS. Rename ("D:/b.txt", "d:/d.txt"); Err! = Nil {panic (err)}} ' **7, get all files for folder * * ' Gofunc test7 () {files, err: = Ioutil. ReadDir ("d:/") if err! = Nil {panic (err)}//gets information about all files and folders under the folder for _, F: = Range files {fmt. Printf ("Name:%s, size:%d, Isdir:%t\n", F.name (), F.size (), F.isdir ())}} ' **8, writes the contents of one file to another file, and can control the writing of the content * * ' Gofunc Test8 () {f1, err: = OS. Open ("D:/a.txt") if err! = Nil {panic (err)}defer F1. Close () F2, err: = OS. OpenFile ("D:/b.txt", OS. O_create | Os. O_rdwr | Os. O_append, 0755) if err! = Nil {panic (err)}defer F2. The Close ()//setting will read from F1 to F2 in r: = Io. Teereader (f1, F2)//Loop 5 reads F1 the front 100 characters, each read will be automatically written to F2 for I: = 0; I < 5; i++ {F1. Seek (0, Io. Seekstart) If _, Err: = R.read (Make ([]byte, 100)); ERR! = Nil {if Err = = Io. Eof{return}panic (Err)}time. Sleep (time. Second)}} "The case is from:" Go language standard library "https://books.studygolang.com/The-Golang-Standard-Library-by-Example/chapter01/ 01.1.html64 Times Click  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.