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))
    }

 

posted on 2015-04-06 14:07  rojas  阅读(44898)  评论(0编辑  收藏  举报