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

Files are composed of ordered bytes, and these bytes are addressable by their position, relative to the beginning of in the file (position zero [0]). Once we have a file descriptor fd, we can begin to read length number of bytes and insert those into a Buffer object buffer, insertion beginning at a given buffer offset. For example, to copy the 8,366 bytes beginning at position 309 of the readable file fd into a buffer beginning at an offset of 100, we will use fs.read(fd, buffer, 100, 8366, 309, callback).

The following code demonstrates how to open and read a file in 512 byte chunks:

fs.open('path.js', 'r', (err, fd) => {   fs.fstat(fd, (err, stats) => {  let totalBytes = stats.size; ...

Get Mastering Node.js - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.