golang 逐行读取文件

package main



import (

    "bufio"

    "fmt"

    "io"

    "os"

)



func main() {



    fi, err := os.Open("C:/Documents and Settings/xxx/Desktop/tax.txt")

    if err != nil {

        fmt.Printf("Error: %s\n", err)

        return

    }

    defer fi.Close()



    br := bufio.NewReader(fi)

    for {

        a, _, c := br.ReadLine()

        if c == io.EOF {

            break

        }

        fmt.Println(string(a))

    }

}

 

================一次性读取=====================



package main



    import "fmt"

    import "io/ioutil"



    func main() {

        b, e := ioutil.ReadFile("d:/goTest/123.txt")

        if e != nil {

            fmt.Println("read file error")

            return

        }

        fmt.Println(string(b))

    }

 

你可能感兴趣的:(golang)