Read one character at a time in node.js?

advertisements

Is there a way to read one symbol at a time in nodejs from file without storing the whole file in memory? I found an answer for lines

I tried something like this but it doesn't help:

stream = fs.createReadStream("walmart.dump", {
    encoding: 'utf8',
    fd: null,
    bufferSize: 1,
});
stream.on('data', function(sym){
    console.log(sym);
});


Readable stream has a read() method, where you can pass a size of readed symbols. Example code:

var readable = fs.createReadStream("walmart.dump", {
    encoding: 'utf8',
    fd: null,
});
readable.on('readable', function() {
  var chunk;
  while (null !== (chunk = readable.read(1))) {
    console.log(chunk); // chunk is one symbol
  }
});