有时候我们在windows上会保存一些中文文字信息文件,然而由于编码集的差异,这文件在linux上显示为乱码,其中一种解决方法是node.js的iconv模块
var fs = require('fs'); var readstream = fs.createReadStream('./新建文本文档.txt'); var str=''; var iconv = require('iconv-lite'); var count=0; readstream.on('data',function (chunk) { str+=iconv.decode(chunk,'GBK'); count++; }); readstream.on('end',function () { console.log(str.toString()); console.log("文件分"+count+"次读完") });