How to read binary file byte by byte using javascript?


I need to read the binary file byte by byte using javascript.I had got below code in this site,but its not working.I think i have to add some extra src file as a reference to it.Please help me to do the code…

var fs = require('fs');
var Buffer = require('buffer').Buffer;
var constants = require('constants');"file.txt", 'r', function(status, fd) {
    if (status) {
    var buffer = new Buffer(100);, buffer, 0, 100, 0, function(err, num) {
        console.log(buffer.toString('utf-8', 0, num));


You can read the file synchronously, byte by byte:'file.txt', 'r', function(err, fd) {
  if (err)
    throw err;
  var buffer = Buffer.alloc(1);
  while (true)
    var num = fs.readSync(fd, buffer, 0, 1, null);
    if (num === 0)
    console.log('byte read', buffer[0]);

Answered By – robertklep

Answer Checked By – Senaida (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.