Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a section of a large file with Node.js?

Tags:

file

io

node.js

I have a very large, binary file (>25 GB), and I need to very quickly read a small range of bytes from it at a specific offset. How can I accomplish this in Node.js in an efficient way?

like image 564
Brenden Avatar asked Oct 19 '25 08:10

Brenden


1 Answers

A fairly minimal example of what you want, refer to https://nodejs.org/api/all.html#fs_fs_createreadstream_path_options for more details

const fs = require("fs");

const stream = fs.createReadStream("test.txt", { start: 1, end: 5 });

stream.on("data", chunk => console.log(chunk.toString()));

Provided you have a file called test.txt of course...

like image 162
Countingstuff Avatar answered Oct 21 '25 21:10

Countingstuff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!