如何将GBK编码的文件读入字符串?

时间:2017-08-21 03:17:29

标签: rust

use std::env;
use std::fs::File;
use std::io::prelude::*;

fn main() {
    let args: Vec<String> = env::args().collect();
    let filename = &args[1];
    let mut f = File::open(filename).expect("file not found");
    let mut contents = String::new();
    f.read_to_string(&mut contents).expect("something went wrong reading the file");
    println!("file content:\n{}", contents);
}

当我尝试读取GBK编码文件时,出现以下错误:

thread 'main' panicked at 'something went wrong reading the file: Error { repr: Custom(Custom { kind: InvalidData, error: StringError("stream did not contain valid UTF-8") }) }', /checkout/src/libcore/result.rs:860

它说流必须包含有效的UTF-8。如何读取GBK文件?

2 个答案:

答案 0 :(得分:4)

你可能想要encoding箱子。

答案 1 :(得分:1)

我想出了如何从GBK编码的文件中逐行读取。

extern crate encoding;

use std::env;
use std::fs::File;
use std::io::prelude::*;
use std::io::BufReader;

use encoding::all::GBK;
use encoding::{Encoding, EncoderTrap, DecoderTrap};

fn main() {
    let args: Vec<String> = env::args().collect();
    let filename = &args[1];

    let mut file = File::open(filename).expect("file not found");
    let reader = BufReader::new(&file);

    let mut lines = reader.split(b'\n').map(|l| l.unwrap());

    for line in lines {
        let decoded_string = GBK.decode(&line, DecoderTrap::Strict).unwrap();
        println!("{}", decoded_string);
    }
}