逐行读取文件(不是 utf-8)? [英] read file(not utf-8) line by line?
问题描述
是否可以逐行读取文件,如果它不是使用 std::io::File
和 std::io::BufReader编码的 utf-8 编码?代码>?
Is it possible to read file line by line, if it is not in utf-8 encoding with std::io::File
and std::io::BufReader
?
我查看 std::io::Lines
并返回 Result
,所以我担心,我是否实现了自己的 BufReader
来做同样的事情,但返回 Vec
,或者我可以重用 std::io::BufReader
以某种方式?
I look at std::io::Lines
and it return Result<String>
, so
I worry, have I implement my own BufReader
that do the same, but return Vec<u8>
instead, or I can reuse std::io::BufReader
in some way?
推荐答案
您不必重新实现 BufReader
本身,它为您的用例提供了所需的方法 read_until
:
You do not have to re-implement BufReader
itself, it provides exactly the method you need for your usecase read_until
:
fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> Result<usize>
您提供自己的Vec
,文件的内容将被附加,直到遇到byte
(0x0A 为LF).
You supply your own Vec<u8>
and the content of the file will be appended until byte
is encountered (0x0A being LF).
有几个潜在的问题:
- 缓冲区不仅可以以 LF 字节结束,还可以以 CR LF 序列结束,
- 在后续调用之间清除
buf
由您决定.
- the buffer may end not only with a LF byte, but with a CR LF sequence,
- it is up to you to clear
buf
between subsequent calls.
一个简单的while let Ok(_) = reader.read_until(0x0A as u8, buffer)
应该让你很容易阅读你的文件.
A simple while let Ok(_) = reader.read_until(0x0A as u8, buffer)
should let you read your file easily enough.
你可以考虑实现一个 std::io::Lines
等价物,它从编码转换为 UTF-8 以提供一个很好的 API,尽管它会降低性能.
You may consider implement a std::io::Lines
equivalent which converts from the encoding to UTF-8 to provide a nice API, though it will have a performance cost.
这篇关于逐行读取文件(不是 utf-8)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!