5

I tried this simply code

var fs = require('fs');
var stream = fs.createReadStream(fileName);
stream.on('readable', function () {
   console.log("readable event called");
} );

And I noticed that if the file is an empty, then the event is not called. Is there elegant way to check that the file is empty?

1
  • 5
    Check the file size using fs.stat() Commented Mar 10, 2016 at 14:33

1 Answer 1

2

You can use streamName.on('end', () => {})

let readStream = fs.createReadStream(path/to/file);
let emptyFile = true;

readStream.once('data', chunk => {
  emptyFile = false;
});

readStream.on('end', () => {
  console.log('Stream ended');
  if (emptyFile) {
    console.log('Empty file');
  }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.