Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fs.createReadStream() at specific position of file

Is it possible to create a stream that reads from a specific position of file in node.js?

I know that I could use a more traditional fs.open / seek / read API, but in that case I need to somehow wrap them in a stream for underlying layers of my application.

like image 671
vdudouyt Avatar asked Nov 14 '16 01:11

vdudouyt


1 Answers

fs.createReadStream() has an option you can pass it to specify the start position for the stream.

let f = fs.createReadStream("myfile.txt", {start: 1000});

You could also open a normal file descriptor with fs.open(), then fs.read() one byte from a position right before where you want the stream to be positioned using the position argument to fs.read() and then you can pass that file descriptor into fs.createReadStream() as an option and the stream will start with that file descriptor and position (though obviously the start option to fs.createReadStream() is a bit simpler).

like image 105
jfriend00 Avatar answered Sep 21 '22 23:09

jfriend00