问题
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