Sunday, 30 April 2017

read a file which is encoded by gbk with readline in node

question

When reading a file which is encoded by gbk with readline in node,I get a wrong content like this '锟揭革拷锟绞o拷锟较革拷锟斤拷千寻 www.lrcgc.com锟斤拷支锟街革拷锟斤拷锟揭革拷锟斤拷锟斤拷LRC锟斤拷锟斤拷锟斤拷锟斤拷锟 斤拷锟截★拷'.

desc

enter code hereconst fs = require('fs');
const path = require('path');
const iconv = require('iconv-lite');
const readline = require('readline');


const filename = path.join(__dirname,'./music.lrc');

let streamReader = fs.createReadStream(filename);
let rl = readline.createInterface({
    input : streamReader
});

rl.on('line',(chunk)=>{
    // console.log(`你拿到的内容是${chunk}`);
    console.log(typeof chunk);
    console.log(chunk); // 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
    // console.log(iconv.encode(chunk,'gbk'));
    // var con = iconv.encode(chunk,'utf8');
    // console.log(iconv.decode(con,'gbk'));
});



via user7943155

No comments:

Post a Comment