• go语言文件操作


    转自:http://www.cnblogs.com/iscode/archive/2012/05/06/2486528.html

    一、建立与打开

    建立文件函数:

    func Create(name string) (file *File, err Error)
     
    func NewFile(fd int, name string) *File
    

    具体见官网:http://golang.org/pkg/os/#Create
    打开文件函数:

    func Open(name string) (file *File, err Error)
     
    func OpenFile(name string, flag int, perm uint32) (file *File, err Error)
    

    具体见官网:http://golang.org/pkg/os/#Open

    二、写文件

    写文件函数:

    func (file *File) Write(b []byte) (n int, err Error)
     
    func (file *File) WriteAt(b []byte, off int64) (n int, err Error)
     
    func (file *File) WriteString(s string) (ret int, err Error)
    

    具体见官网:http://golang.org/pkg/os/#File.Write
    写文件示例代码:

    package main
    import (
            "os"
            "fmt"
    )
    func main() {
            userFile := "test.txt"
            fout,err := os.Create(userFile)
            defer fout.Close()
            if err != nil {
                    fmt.Println(userFile,err)
                    return
            }
            for i:= 0;i<10;i++ {
                    fout.WriteString("Just a test!
    ")
                    fout.Write([]byte("Just a test!
    "))
            }
    }
    

    三、读文件

    读文件函数:

    func (file *File) Read(b []byte) (n int, err Error)
     
    func (file *File) ReadAt(b []byte, off int64) (n int, err Error)
    

    具体见官网:http://golang.org/pkg/os/#File.Read
    读文件示例代码:

    package main
    import (
            "os"
            "fmt"
    )
    func main() {
            userFile := "test.txt"
            fin,err := os.Open(userFile)
            defer fin.Close()
            if err != nil {
                    fmt.Println(userFile,err)
                    return
            }
            buf := make([]byte, 1024)
            for{
                    n, _ := fin.Read(buf)
                    if0 == n { break }
                    os.Stdout.Write(buf[:n])
            }
    }
    

    四、删除文件

    函数:

    func Remove(name string) Error
    

    使用os库os.Open os.Create

    package main
     
    import (
        "io"
        "os"
    )
     
    func main() {
        fi, err := os.Open("input.txt")
        if err != nil { panic(err) }
        defer fi.Close()
     
        fo, err := os.Create("output.txt")
        if err != nil { panic(err) }
        defer fo.Close()
     
        buf := make([]byte, 1024)
        for {
            n, err := fi.Read(buf)
            if err != nil && err != io.EOF { panic(err) }
            if n == 0 { break }
     
            if n2, err := fo.Write(buf[:n]); err != nil {
                panic(err)
            } else if n2 != n {
                panic("error in writing")
            }
        }
    }
    

    使用bufio库

    package main
     
    import (
        "bufio"
        "io"
        "os"
    )
     
    func main() {
        fi, err := os.Open("input.txt")
        if err != nil { panic(err) }
        defer fi.Close()
        r := bufio.NewReader(fi)
     
        fo, err := os.Create("output.txt")
        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) }
    }
    

    使用ioutil库

    package main
     
    import (
        "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) }
    }
    

    五、遍历文件夹

    package main
    import (
        "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
    ", err)
            }
    }
    
    func main(){
            flag.Parse()
            root := flag.Arg(0)
            getFilelist(root)
    }
    
  • 相关阅读:
    48. Rotate Image
    47. Permutations II
    46. Permutations
    45. Jump Game II
    44. Wildcard Matching
    43. Multiply Strings
    42. Trapping Rain Water
    41. First Missing Positive
    40. Combination Sum II
    39. Combination Sum
  • 原文地址:https://www.cnblogs.com/zheng-chuang/p/6058824.html
Copyright © 2020-2023  润新知