path{string|Buffer|URL|integer} 文件名或文件描述符。options{Object|string}encoding{string|null} 默认值:null。flag{string} 参见[文件系统flag的支持][support of file systemflags]。默认值:'r'。
callback{Function}err{Error}data{string|Buffer}
异步地读取文件的全部内容。
fs.readFile('文件名', (err, data) => {if (err) throw err;console.log(data);});
回调会传入两个参数 (err, data),其中 data 是文件的内容。
如果没有指定字符编码,则返回原始的 buffer。
如果 options 是字符串,则它指定字符编码:
fs.readFile('文件名', 'utf8', callback);
当路径是目录时,则 fs.readFile() 和 [fs.readFileSync()] 的行为是特定于平台的。
在 macOS、Linux 和 Windows 上,会返回错误。
在 FreeBSD 上,会返回目录内容的表示。
// 在 macOS、Linux 和 Windows 上:fs.readFile('<目录>', (err, data) => {// => [Error: EISDIR: illegal operation on a directory, read <目录>]});// 在 FreeBSD 上:fs.readFile('<目录>', (err, data) => {// => null, <data>});
fs.readFile() 函数会缓冲整个文件。
若要最小化内存成本,则尽可能选择流式(使用 fs.createReadStream())。
