在Go语言中,是否有类似于ReadLine的方法用于读取文件?

huangapple go评论125阅读模式
英文:

Is there a ReadLine equivalent for a file in Go?

问题

你可以使用ioutil.ReadFile函数来读取文本文件的内容,然后使用bytes.Split函数将内容按照换行符进行分割,最后取得第一个分割后的部分即可。这样就可以实现在不使用bufio的情况下读取文件直到遇到换行符为止。以下是示例代码:

  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. )
  7. func main() {
  8. filePath := "your_file_path.txt"
  9. content, err := ioutil.ReadFile(filePath)
  10. if err != nil {
  11. fmt.Println("读取文件失败:", err)
  12. return
  13. }
  14. lines := bytes.Split(content, []byte("\n"))
  15. if len(lines) > 0 {
  16. firstLine := lines[0]
  17. fmt.Println(string(firstLine))
  18. }
  19. }

请将your_file_path.txt替换为你要读取的文件路径。这段代码会读取文件的内容,并将其按照换行符进行分割,然后输出第一行的内容。

英文:

I want to ReadBytes until "\n" for a text file, not a bufio.

Is there a way to do this without converting to a bufio?

答案1

得分: 2

有很多方法可以实现,但我建议使用bufio进行包装。但如果这对你不起作用(为什么不起作用?),你可以继续像这样逐个字节地读取:

完整的工作示例:

  1. package main
  2. import (
  3. "bufio"
  4. "bytes"
  5. "fmt"
  6. "io"
  7. )
  8. // ReadLine 从 io.Reader 中读取以 \n 分隔的一行
  9. // 与 bufio 不同,它通过逐个字节地读取来实现,效率较低
  10. func ReadLine(r io.Reader) (line []byte, err error) {
  11. b := make([]byte, 1)
  12. var l int
  13. for err == nil {
  14. l, err = r.Read(b)
  15. if l > 0 {
  16. if b[0] == '\n' {
  17. return
  18. }
  19. line = append(line, b...)
  20. }
  21. }
  22. return
  23. }
  24. var data = `Hello, world!
  25. I will write
  26. three lines.`
  27. func main() {
  28. b := bytes.NewBufferString(data)
  29. for {
  30. line, err := ReadLine(b)
  31. fmt.Println("Line: ", string(line))
  32. if err != nil {
  33. return
  34. }
  35. }
  36. }

输出:

  1. Line: Hello, world!
  2. Line: I will write
  3. Line: three lines.

Playground: http://play.golang.org/p/dfb0GHPpnm

英文:

There are many ways to do it, but wrapping with bufio is what I would suggest. But if that doesn't work for you (why not?), you can go ahead and read single bytes like this:

Full working example:

  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. )
  7. // ReadLine reads a line delimited by \n from the io.Reader
  8. // Unlike bufio, it does so rather inefficiently by reading one byte at a time
  9. func ReadLine(r io.Reader) (line []byte, err error) {
  10. b := make([]byte, 1)
  11. var l int
  12. for err == nil {
  13. l, err = r.Read(b)
  14. if l > 0 {
  15. if b[0] == '\n' {
  16. return
  17. }
  18. line = append(line, b...)
  19. }
  20. }
  21. return
  22. }
  23. var data = `Hello, world!
  24. I will write
  25. three lines.`
  26. func main() {
  27. b := bytes.NewBufferString(data)
  28. for {
  29. line, err := ReadLine(b)
  30. fmt.Println("Line: ", string(line))
  31. if err != nil {
  32. return
  33. }
  34. }
  35. }

Output:

  1. Line: Hello, world!
  2. Line: I will write
  3. Line: three lines.

Playground: http://play.golang.org/p/dfb0GHPpnm

huangapple
  • 本文由 发表于 2014年4月25日 11:15:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/23283549.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定