问题
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.
回答1:
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).
来源:https://stackoverflow.com/questions/40580809/fs-createreadstream-at-specific-position-of-file