Reading proper unicode characters into a ReadStream in node.js

孤者浪人 提交于 2021-01-28 11:47:49

问题


Sometimes strange things happen in the world of coding, and I have no explanation at all. :)

A text file I have contains the following lines:

en …π 1 1
en Ψ 1 1
en Ω 1 1
en –° 1 1
en —† 1 1
en Ҥ 1 1
en ◊° 2 2
en Ḥanīf 1 1
en ·π_ 1 1
en ˝mage:whiteshark-tgoss1.jpg 4 4
en ˝stanbul 114 114

My code is as follows:

var fileReadStream = fs.createReadStream(fileName, {encoding: 'utf8'});
fileReadStream.on('data', function(data){
  //do something with the data
});

When I look at the data element, all the crazy unicode characters are replaced with �s. ie:

en �� 1 1
en �� 1 1
en �� 1 1
en �� 1 1
en �� 1 1
en �� 1 1
en �� 2 2
en ���an��f 1 1
en ��_ 1 1
en �mage:whiteshark-tgoss1.jpg 4 4
en �stanbul 114 114

I've tried setting up the ReadStream without the encoding set to UTF8 as well, with no luck.

var fileReadStream = fs.createReadStream(fileName);

Why is this happening?

来源:https://stackoverflow.com/questions/18362657/reading-proper-unicode-characters-into-a-readstream-in-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!