Home >Web Front-end >JS Tutorial >Instructions for using the fs.read method in node.js_node.js
Method description:
Read file data based on the specified file descriptor fd and write the buffer object pointed to by buffer. It provides a lower-level interface than readFile.
It is generally not recommended to use this method to read files, because it requires you to manually manage the buffer and file pointer, especially when you do not know the file size, this will be a very troublesome thing .
Grammar:
Since this method belongs to the fs module, the fs module needs to be introduced before use (var fs= require(“fs”) )
Receive parameters:
fs File descriptor
buffer The buffer into which data will be written.
offset The offset written to the buffer
length (integer) Specifies the length of the file read in bytes
position (integer) Specifies the starting position of file reading. If this item is null, data will be read starting from the position of the current file pointer.
callback The callback passes three parameters, err, bytesRead, buffer
· err Exception
· bytesRead: Number of bytes read
·buffer: buffer object
Example:
Source code: