fs.read(fd, buffer, offset, length, position, callback)
-
fd
<integer> -
buffer
<Buffer> | <TypedArray> | <DataView> 数据将写入的缓冲区。¥
buffer
<Buffer> | <TypedArray> | <DataView> The buffer that the data will be written to. -
offset
<integer> 要写入数据的buffer
中的位置。¥
offset
<integer> The position inbuffer
to write the data to. -
length
<integer> 读取的字节数。¥
length
<integer> The number of bytes to read. -
position
<integer> | <bigint> | <null> 指定从文件中开始读取的位置。如果position
为null
或-1
,则将从当前文件位置读取数据,并更新文件位置。如果position
是整数,则文件位置将保持不变。¥
position
<integer> | <bigint> | <null> Specifies where to begin reading from in the file. Ifposition
isnull
or-1
, data will be read from the current file position, and the file position will be updated. Ifposition
is an integer, the file position will be unchanged. -
callback
<Function>
从 fd
指定的文件中读取数据。
¥Read data from the file specified by fd
.
回调被赋予三个参数,(err, bytesRead, buffer)
。
¥The callback is given the three arguments, (err, bytesRead, buffer)
.
如果未同时修改文件,当读取的字节数为零时,则到达文件末尾。
¥If the file is not modified concurrently, the end-of-file is reached when the number of bytes read is zero.
如果此方法作为其 util.promisify()
版本被调用,则返回具有 bytesRead
和 buffer
属性的 Object
的 promise。
¥If this method is invoked as its util.promisify()
ed version, it returns
a promise for an Object
with bytesRead
and buffer
properties.
fs.read()
方法从文件描述符 (fd
) 指定的文件中读取数据。length
参数指示 Node.js 将尝试从内核读取的最大字节数。然而,由于各种原因,实际读取的字节数 (bytesRead
) 可能低于指定的 length
。
¥The fs.read()
method reads data from the file specified
by the file descriptor (fd
).
The length
argument indicates the maximum number
of bytes that Node.js
will attempt to read from the kernel.
However, the actual number of bytes read (bytesRead
) can be lower
than the specified length
for various reasons.
例如:
¥For example:
-
如果文件比指定的
length
短,bytesRead
将被设置为实际读取的字节数。¥If the file is shorter than the specified
length
,bytesRead
will be set to the actual number of bytes read. -
如果文件在缓冲区被填充之前遇到 EOF(文件结束),Node.js 将读取所有可用字节,直到遇到 EOF,回调中的
bytesRead
参数将指示实际读取的字节数,该数可能会更少比指定的length
。¥If the file encounters EOF (End of File) before the buffer could be filled, Node.js will read all available bytes until EOF is encountered, and the
bytesRead
parameter in the callback will indicate the actual number of bytes read, which may be less than the specifiedlength
. -
如果文件位于慢速网络
filesystem
上或在读取过程中遇到任何其他问题,则bytesRead
可以低于指定的length
。¥If the file is on a slow network
filesystem
or encounters any other issue during reading,bytesRead
can be lower than the specifiedlength
.
因此,在使用 fs.read()
时,检查 bytesRead
值以确定实际从文件中读取了多少字节非常重要。根据你的应用逻辑,你可能需要处理 bytesRead
低于指定的 length
的情况,例如,如果你需要最小数量的字节,则将读取调用封装在循环中。
¥Therefore, when using fs.read()
, it's important to
check the bytesRead
value to
determine how many bytes were actually read from the file.
Depending on your application
logic, you may need to handle cases where bytesRead
is lower than the specified length
,
such as by wrapping the read call in a loop if you require
a minimum amount of bytes.
此行为类似于 POSIX preadv2
函数。
¥This behavior is similar to the POSIX preadv2
function.