How do I do random access reads from (large) files using node.js?

徘徊边缘 提交于 2019-12-04 07:32:18

tell is not, but it is pretty rare to not already know the position you are at in a file, or to not have a way to keep track yourself.

seek is exposed indirectly via the position argument of fs.read and fs.write. When given, the argument will seek to that location before performing its operation, and if null, it will use whatever previous position it had.

yiding

node doesn't have these built in, the closest you can get is to use fs.createReadStream with a start parameter to start reading from an offset, (pass in an existing fd to avoid re-opening the file).

http://nodejs.org/api/fs.html#fs_fs_createreadstream_path_options

I suppose that createReadStream creates new file descriptor over and over. I prefer sync version:

function FileBuffer(path) {
const fd = fs.openSync(path, 'r');

function slice(start, end) {
    const chunkSize = end - start;
    const buffer = new Buffer(chunkSize);

    fs.readSync(fd, buffer, 0, chunkSize, start);

    return buffer;
}

function close() {
    fs.close(fd);
}

return {
    slice,
    close
}

}

Use this:

fs.open(path, flags[, mode], callback)

Then this:

fs.read(fd, buffer, offset, length, position, callback)

Read this for details:

https://nodejs.org/api/fs.html#fs_fs_read_fd_buffer_offset_length_position_callback

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