Using NodeJS v5.6 I created a file called read-stream.js
:
const
fs = require('fs'),
stream = fs.createReadStream(process.argv[2]);
stream.on('data', function(chunk) {
process.stdout.write(chunk);
});
stream.on('error', function(err) {
process.stderr.write("ERROR: " + err.message + "\n");
});
and a data file in plain text called target.txt
:
hello world
this is the second line
If I do node read-stream.js target.txt
the contents of target.txt
are printed normally on my console and all is well.
However if I switch process.stdout.write(chunk);
with console.log(chunk);
then the result I get is this:
<Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64 0a 74 68 69 73 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20 6c 69 6e 65 0a>
I recently found out that by doing console.log(chunk.toString());
the contents of my file are once again printed normally.
As per this question, console.log
is supposed to use process.stdout.write
with the addition of a \n
character. But what exactly is happening with encoding/decodings here?
Thanks in advance.