fs.readFile(path[, options], callback)
path<string> | <Buffer> | <URL> | <integer> 文件名或文件描述符options<Object> | <string>encoding<string> | <null> 默认:nullflag<string> 参见 对文件系统flags的支持。默认值:'r'。signal<AbortSignal> 允许中止正在进行的 readFile 操作buffer<Buffer> | <TypedArray> | <DataView> | <Function> 一个用于读取的缓冲区,或者一个以文件大小为参数返回缓冲区的函数。
callback<Function>err<Error> | <AggregateError>data<string> | <Buffer>
异步地读取文件的全部内容。
🌐 Asynchronously reads the entire contents of a file.
import { readFile } from 'node:fs';
readFile('/etc/passwd', (err, data) => {
if (err) throw err;
console.log(data);
}); 回调函数会传入两个参数 (err, data),其中 data 是文件的内容。
🌐 The callback is passed two arguments (err, data), where data is the
contents of the file.
如果未指定编码,则返回原始缓冲区。
🌐 If no encoding is specified, then the raw buffer is returned.
如果提供了 buffer 且未指定编码,返回的 <Buffer> 将是一个只包含已读取字节的缓冲区视图。如果提供的缓冲区太小而无法容纳整个文件,将会调用回调并返回错误。
🌐 If buffer is provided and no encoding is specified, the returned <Buffer> is
a view over the supplied buffer containing only the bytes read. If the
supplied buffer is too small to contain the entire file, the callback is
called with an error.
如果 options 是字符串,则它指定了编码方式:
🌐 If options is a string, then it specifies the encoding:
import { readFile } from 'node:fs';
readFile('/etc/passwd', 'utf8', callback); 当路径是一个目录时,fs.readFile() 和 fs.readFileSync() 的行为取决于平台。在 macOS、Linux 和 Windows 上,会返回一个错误。在 FreeBSD 上,会返回目录内容的表示。
🌐 When the path is a directory, the behavior of fs.readFile() and
fs.readFileSync() is platform-specific. On macOS, Linux, and Windows, an
error will be returned. On FreeBSD, a representation of the directory's contents
will be returned.
import { readFile } from 'node:fs';
// macOS, Linux, and Windows
readFile('<directory>', (err, data) => {
// => [Error: EISDIR: illegal operation on a directory, read <directory>]
});
// FreeBSD
readFile('<directory>', (err, data) => {
// => null, <data>
}); 可以使用 AbortSignal 中止正在进行的请求。如果请求被中止,回调将使用 AbortError 调用:
🌐 It is possible to abort an ongoing request using an AbortSignal. If a
request is aborted the callback is called with an AbortError:
import { readFile } from 'node:fs';
const controller = new AbortController();
const signal = controller.signal;
readFile(fileInfo[0].name, { signal }, (err, buf) => {
// ...
});
// When you want to abort the request
controller.abort(); fs.readFile() 函数会将整个文件缓存在内存中。为了尽量减少内存消耗,在可能的情况下,建议使用 fs.createReadStream() 进行流式处理。
🌐 The fs.readFile() function buffers the entire file. To minimize memory costs,
when possible prefer streaming via fs.createReadStream().
中止正在进行的请求并不会中止单个操作系统请求,而是中止内部缓冲 fs.readFile 所执行的操作。
🌐 Aborting an ongoing request does not abort individual operating
system requests but rather the internal buffering fs.readFile performs.
使用预分配缓冲区和 buffer 选项的示例:
🌐 An example using the buffer option with a pre-allocated buffer:
import { Buffer } from 'node:buffer';
import { readFile } from 'node:fs';
const buf = Buffer.alloc(16384);
readFile('/path/to/file', { buffer: buf }, (err, data) => {
if (err) throw err;
console.log(data); // A view over `buf` containing only the bytes read
}); 使用 buffer 选项和返回缓冲区的函数的示例:
🌐 An example using the buffer option with a function returning a buffer:
import { Buffer } from 'node:buffer';
import { readFile } from 'node:fs';
readFile('/path/to/file', {
buffer: (size) => Buffer.alloc(size),
}, (err, data) => {
if (err) throw err;
console.log(data);
});