fs.readFile(path[, options], callback)
版本 | 變化 |
---|---|
v18.0.0 | 將無效回調傳遞給 |
v16.0.0 | 如果返回多個錯誤,則返回的錯誤可能是 |
v15.2.0、v14.17.0 | options 參數可能包含 AbortSignal 以中止正在進行的 readFile 請求。 |
v10.0.0 |
|
v7.6.0 |
|
v7.0.0 |
|
v5.1.0 | 如果成功, |
v5.0.0 |
|
v0.1.29 | 添加於:v0.1.29 |
參數
path
<string> | <Buffer> | <URL> | <integer> 文件名或文件說明符options
<Object>|<string>encoding
<string> | <null> 默認:null
flag
<string>參看支持文件係統flags
.默認:'r'
.signal
<AbortSignal> 允許中止 in-progress 讀取文件
callback
<Function>err
<Error> | <AggregateError>data
<string> | <Buffer>
異步讀取文件的全部內容。
import { readFile } from 'node:fs';
readFile('/etc/passwd', (err, data) => {
if (err) throw err;
console.log(data);
});
回調傳遞了兩個參數 (err, data)
,其中 data
是文件的內容。
如果未指定編碼,則返回原始緩衝區。
如果options
是字符串,則它指定編碼:
import { readFile } from 'node:fs';
readFile('/etc/passwd', 'utf8', callback);
當路徑是目錄時,fs.readFile()
和
的行為是特定於平台的。在 macOS、Linux 和 Windows 上,將返回錯誤。在 FreeBSD 上,將返回目錄內容的表示。fs.readFileSync()
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
調用回調:
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()
進行流式傳輸。
中止正在進行的請求不會中止單個操作係統請求,而是執行內部緩衝fs.readFile
。
文件說明符#
- 任何指定的文件說明符都必須支持讀取。
- 如果文件說明符被指定為
path
,它不會自動關閉。 - 讀數將從當前位置開始。例如,如果文件已經有
'Hello World
' 並且使用文件說明符讀取了六個字節,則使用相同的文件說明符調用fs.readFile()
將給出'World'
,而不是'Hello World'
。
性能注意事項#
fs.readFile()
方法一次將一個文件的內容異步讀取到內存中,允許事件循環在每個塊之間切換。這允許讀取操作對可能使用底層 libuv 線程池的其他活動的影響較小,但意味著將完整文件讀入內存需要更長的時間。
額外的讀取開銷在不同的係統上可能會有很大差異,並且取決於正在讀取的文件類型。如果文件類型不是常規文件(例如管道)並且 Node.js 無法確定實際文件大小,則每次讀取操作將加載 64 KiB 個數據。對於常規文件,每次讀取將處理 512 KiB 個數據。
對於需要as-fast-as-possible 讀取文件內容的應用程序,最好直接使用fs.read()
並且應用程序代碼管理讀取文件本身的全部內容。
Node.js GitHub 問題#25741 提供了更多信息和關於fs.readFile()
在不同 Node.js 版本中針對多種文件大小的性能的詳細分析。
相關用法
- Node.js fs.readFile()用法及代碼示例
- Node.js fs.readFileSync()用法及代碼示例
- Node.js fs.readFileSync(path[, options])用法及代碼示例
- Node.js fs.read()用法及代碼示例
- Node.js fs.readdir()用法及代碼示例
- Node.js fs.readlinkSync()用法及代碼示例
- Node.js fs.readdirSync()用法及代碼示例
- Node.js fs.readlink()用法及代碼示例
- Node.js fs.realpath()用法及代碼示例
- Node.js fs.realpathSync()用法及代碼示例
- Node.js fs.rename()用法及代碼示例
- Node.js fs.renameSync()用法及代碼示例
- Node.js fs.rename(oldPath, newPath, callback)用法及代碼示例
- Node.js fs.rmdir()用法及代碼示例
- Node.js fs.rm()用法及代碼示例
- Node.js fs.rmdirSync()用法及代碼示例
- Node.js fs.rmSync()用法及代碼示例
- Node.js fs.filehandle.datasync()用法及代碼示例
- Node.js fs.chmod()用法及代碼示例
- Node.js fs.Dirent.isFile()用法及代碼示例
- Node.js fs.Dir.closeSync()用法及代碼示例
- Node.js fs.fchmodSync()用法及代碼示例
- Node.js fs.symlink(target, path[, type], callback)用法及代碼示例
- Node.js fs.constants用法及代碼示例
- Node.js fs.mkdir()用法及代碼示例
注:本文由純淨天空篩選整理自nodejs.org大神的英文原創作品 fs.readFile(path[, options], callback)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。