How to read last N bytes of file using NodeJs

how to read last n bytes of file using node js

Node.Js is a great framework to build I/O applications where there are too many database interactions. Sometimes we need to read bytes of files using Node.Js. In this post, we will cover how to read last n bytes of file using node.js.


Table of Contents

Step 1

Create a file named app.js and require fs module in the file.

Step 2

Use fs.stat to get the file size.

Step 3

Open and read the file using fs.open and fs.read with file size, position etc values.

Step 4

The complete code goes here-

 

Here are the contents of my test file-

how to read last n bytes of file using node js

Result after reading last 10 bytes-

how to read last n bytes of file using node js

Share your issues/concerns if you get any while reading the bytes of file.

Thanks

1 thought on “How to read last N bytes of file using NodeJs

Leave a Reply

Your email address will not be published. Required fields are marked *