fs.createReadStream() at specific position of file

扶醉桌前 提交于 2021-02-07 13:19:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!