這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
1.相關api介紹
1.1建立File記憶體位址api
//返回File的記憶體位址,錯誤資訊,通過os庫調用func Create(name string) (file *File, err Error)//返迴文件的記憶體位址,通過os庫調用func NewFile(fd int, name string) *File
1.2開啟檔案api
//返回File的記憶體位址,錯誤資訊,通過os庫調用func Open(name string)(file *File,err Error)//返回File的記憶體位址,錯誤資訊,通過os庫調用func OpenFile(name string,flag int,perm unit32)(file *File,err Error)
1.3寫檔案api
//寫入一個slice,返回寫的個數,錯誤資訊,通過File的記憶體位址調用func (file *File)Write(b []byte)(n int,err Error)//從slice的某個位置開始寫入,返回寫的個數,錯誤資訊,通過File的記憶體位址調用func (file *File)WriteAt(b []byte,off int64)(n int,err Error)//寫入一個字串,返回寫的個數,錯誤資訊,通過File的記憶體位址調用func (file *File) WriteString(s string)(ret int,err Error)
1.4讀檔案api
//讀取一個slice,返回讀的個數,錯誤資訊,通過File的記憶體位址調用func (file *File) Read(b []byte)(n int, err Error)//從slice的某個位置開始讀取,返回讀到的個數,錯誤資訊,通過File的記憶體位址調用func (file *File) ReadAt(b []byte,off int64)(n int,err Error)
1.5刪除檔案api
//傳入檔案的路徑來刪除檔案,返回錯誤個數func Remove(name string) Error
2.寫檔案的執行個體代碼
package mainimport ( "fmt" "os")func main() { userFile := "d:/test.txt" //檔案路徑 fout,err := os.Create(userFile) //根據路徑建立File的記憶體位址 defer fout.Close() //延遲關閉資源 if err != nil{ fmt.Println(userFile,err) return } //迴圈寫入資料到檔案 for i:=0;i<10;i++{ fout.WriteString("Hello world!\r\n") //寫入字串 fout.Write([]byte("abcd!\r\n"))//強轉成byte slice後再寫入 }}
輸出的檔案內容:
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.讀檔案的執行個體代碼
package mainimport ( "fmt" "os")func main() { userFile := "d:/test.txt" //檔案路徑 fin,err := os.Open(userFile) //開啟檔案,返回File的記憶體位址 defer fin.Close() //延遲關閉資源 if err != nil{ fmt.Println(userFile,err) return } buf := make([]byte,1024)//建立一個初始容量為1024的slice,作為緩衝容器 for{ //迴圈讀取檔案資料到緩衝容器中,返回讀取到的個數 n,_ := fin.Read(buf) if 0==n{ break //如果讀到個數為0,則讀取完畢,跳出迴圈 } //從緩衝slice中寫出資料,從slice下標0到n,通過os.Stdout寫出到控制台 os.Stdout.Write(buf[:n]) }}
運行結果:
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.同時使用os.Open和os.Create操作檔案
package mainimport ( "io" "os")func main() { fi, err := os.Open("d:/input.txt")//開啟輸入*File if err != nil { panic(err) } defer fi.Close() fo, err := os.Create("d:/output.txt")//建立輸出*File if err != nil { panic(err) } defer fo.Close() buf := make([]byte, 1024) for { n, err := fi.Read(buf)//從input.txt讀取 if err != nil && err != io.EOF { panic(err) } if n == 0 { break } if n2, err := fo.Write(buf[:n]); err != nil {//寫入output.txt,直到錯誤 panic(err) } else if n2 != n { panic("error in writing") } }}
5.使用bufio庫
package mainimport ( "bufio" "io" "os")func main() { fi, err := os.Open("input.txt")//開啟輸入*File if err != nil { panic(err) } defer fi.Close() r := bufio.NewReader(fi)//建立一個讀取緩衝流 fo, err := os.Create("output.txt")//建立輸出*File if err != nil { panic(err) } defer fo.Close() w := bufio.NewWriter(fo)//建立輸出緩衝流 buf := make([]byte, 1024) for { n, err := r.Read(buf) if err != nil && err != io.EOF { panic(err) } if n == 0 { break } if n2, err := w.Write(buf[:n]); err != nil { panic(err) } else if n2 != n { panic("error in writing") } } if err = w.Flush(); err != nil { panic(err) }}
6.使用ioutil庫
package mainimport ( "io/ioutil")func main() { b, err := ioutil.ReadFile("input.txt")//讀檔案 if err != nil { panic(err) } err = ioutil.WriteFile("output.txt", b, 0644)//寫檔案 if err != nil { panic(err) }}
7.遍曆檔案夾
package mainimport ( "path/filepath" "os" "fmt" "flag")func getFilelist(path string) { err := filepath.Walk(path, func(path string, f os.FileInfo, err error) error { if ( f == nil ) {return err} if f.IsDir() {return nil} println(path) return nil }) if err != nil { fmt.Printf("filepath.Walk() returned %v\n", err) }}func main(){ flag.Parse() root := flag.Arg(0) getFilelist(root)}
運行結果: