path
<string> | <Buffer> | <URL> | <integer> 文件名或文件描述符。options
<Object> | <string>
callback
<Function>異步地讀取一個文件的所有內容。 例子:html
fs.readFile('/etc/passwd', (err, data) => { if (err) throw err; console.log(data); });
回調有兩個參數 (err, data)
,其中 data
是文件的內容。node
若是未指定字符編碼,則返回原始的 buffer。linux
若是 options
是一個字符串,則它指定了字符編碼。 例子:git
fs.readFile('/etc/passwd', 'utf8', callback);
注意:當 path
是一個目錄時,fs.readFile()
與 fs.readFileSync()
的行爲與平臺有關。 在 macOS、Linux 與 Windows 上,會返回一個錯誤。 在 FreeBSD 上,會返回目錄內容的表示。github
// 在 macOS、Linux 與 Windows 上: fs.readFile('<directory>', (err, data) => { // => [Error: EISDIR: illegal operation on a directory, read <directory>] }); // 在 FreeBSD 上: fs.readFile('<directory>', (err, data) => { // => null, <data> });
任何指定的文件描述符必須支持讀取。windows
注意:若是一個文件描述符被指定爲 path
,則它不會被自動關閉。api
fs.readFile()
的同步版本。 返回 path
的內容。安全
若是指定了 encoding
選項,則該函數返回一個字符串,不然返回一個 buffer。網絡
請注意: 與fs.readFile()
類似, 當路徑是目錄時,fs.readFileSync()
的行爲是基於平臺的。app
// macOS, Linux 和 Windows fs.readFileSync('<directory>'); // => [Error: EISDIR: illegal operation on a directory, read <directory>] // FreeBSD fs.readFileSync('<directory>'); // => null, <data>
path
<string> | <Buffer> | <URL>options
<string> | <Object>
encoding
<string> 默認 = 'utf8'
callback
<Function>異步的 readlink(2)。 回調有兩個參數 (err, linkString)
。
可選的 options
參數用於傳入回調的連接路徑,它能夠是一個字符串並指定一個字符編碼,或是一個對象且由一個 encoding
屬性指定使用的字符編碼。 若是 encoding
設爲 'buffer'
,則返回的連接路徑會被做爲 Buffer
對象傳入。
同步的 readlink(2)。 返回符號連接的字符串值。
可選的 options
參數用於傳入回調的連接路徑,它能夠是一個字符串並指定一個字符編碼,或是一個對象且由一個 encoding
屬性指定使用的字符編碼。 若是 encoding
設爲 'buffer'
,則返回的連接路徑會被做爲 Buffer
對象傳入。
fd
<integer>buffer
<string> | <Buffer> | <Uint8Array>offset
<integer>length
<integer>position
<integer>fs.read()
的同步版本。 返回 bytesRead
的數量。
path
<string> | <Buffer> | <URL>options
<string> | <Object>
encoding
<string> 默認 = 'utf8'
callback
<Function>異步的 realpath(3)。 callback
有兩個參數 (err, resolvedPath)
。 可使用 process.cwd
解析相對路徑。
只支持可轉換成 UTF8 字符串的路徑。
可選的 options
參數用於傳入回調的路徑,它能夠是一個字符串並指定一個字符編碼,或是一個對象且由一個 encoding
屬性指定使用的字符編碼。 若是 encoding
設爲 'buffer'
,則返回的路徑會被做爲 Buffer
對象傳入。
Note: If path
resolves to a socket or a pipe, the function will return a system dependent name for that object.
同步的 realpath(3)。 返回解析的路徑。
只支持可轉換成 UTF8 字符串的路徑。
可選的 options
參數用於傳入回調的路徑,它能夠是一個字符串並指定一個字符編碼,或是一個對象且由一個 encoding
屬性指定使用的字符編碼。 若是 encoding
設爲 'buffer'
,則返回的路徑會被做爲 Buffer
對象傳入。
Note: If path
resolves to a socket or a pipe, the function will return a system dependent name for that object.
異步的 rename(2)。 完成回調只有一個可能的異常參數。
同步的 rename(2)。返回 undefined
。
path
<string> | <Buffer> | <URL>callback
<Function>異步的 rmdir(2)。 完成回調只有一個可能的異常參數。
請注意: 在文件上(而不是目錄上)使用fs.rmdir()
,在Windows平臺將會致使ENOENT
錯誤,而在POSIX平臺將會致使ENOTDIR
錯誤。
同步的 rmdir(2)。返回 undefined
。
請注意: 在文件上(而不是目錄上)使用fs.rmdirSync()
,在Windows平臺將會致使ENOENT
錯誤,而在POSIX平臺將會致使ENOTDIR
錯誤。
path
<string> | <Buffer> | <URL>callback
<Function>異步的 stat(2)。 回調有兩個參數 (err, stats)
其中 stats
是一個 fs.Stats
對象。
若是發生錯誤,則 err.code
會是常見系統錯誤之一。
不建議在調用 fs.open()
、fs.readFile()
或 fs.writeFile()
以前使用 fs.stat()
檢查一個文件是否存在。 做爲替代,用戶代碼應該直接打開/讀取/寫入文件,當文件無效時再處理錯誤。
若是要檢查一個文件是否存在且不操做它,推薦使用 fs.access()
。
同步的 stat(2)。 返回一個 fs.Stats
實例。
target
<string> | <Buffer> | <URL>path
<string> | <Buffer> | <URL>type
<string> Default: 'file'
callback
<Function>異步的 symlink(2)。 完成回調只有一個可能的異常參數。 type
參數能夠設爲 'dir'
、'file'
或 'junction'
(默認爲 'file'
),且僅在 Windows 上有效(在其餘平臺上忽略)。 注意,Windows 結點要求目標路徑是絕對的。 當使用 'junction'
時,target
參數會被自動標準化爲絕對路徑。
例子:
fs.symlink('./foo', './new-port', callback);
它建立了一個名爲 "new-port" 且指向 "foo" 的符號連接。
同步的 symlink(2)。返回 undefined
。
path
<string> | <Buffer>len
<integer> 默認 = 0
callback
<Function>異步的 truncate(2)。 完成回調只有一個可能的異常參數。 文件描述符也能夠做爲第一個參數傳入,在這種狀況下,fs.ftruncate()
會被調用。
同步的 truncate(2)。 返回 undefined
。 文件描述符也能夠做爲第一個參數傳入,在這種狀況下,fs.ftruncateSync()
會被調用。
path
<string> | <Buffer> | <URL>callback
<Function>異步的 unlink(2)。 完成回調只有一個可能的異常參數。
同步的 unlink(2)。返回 undefined
。
filename
<string> | <Buffer>listener
<Function> | <undefined> Default: undefined
中止監視 filename
文件的變化。 若是指定了 listener
,則只移除特定的監聽器。 不然,全部的監聽器都會被移除,且已經有效地中止監視 filename
。
調用 fs.unwatchFile()
且帶上一個未被監視的文件名,將會是一個空操做,而不是一個錯誤。
注意:fs.watch()
比 fs.watchFile()
和 fs.unwatchFile()
更高效。 可能的話,應該使用 fs.watch()
而不是 fs.watchFile()
和 fs.unwatchFile()
。
改變指定的路徑所指向的文件的文件時間戳。
注意:atime
和 mtime
參數遵循如下規則:
Date.now()
返回毫秒,因此在傳入前應該除以1000。'123456789'
,則該值會被轉換爲對應的數值。NaN
、 Infinity
或 -Infinity
,則會拋出錯誤。fs.utimes()
的同步版本。返回 undefined
。
filename
<string> | <Buffer> | <URL>options
<string> | <Object>
listener
<Function> | <undefined> Default: undefined
監視 filename
的變化,filename
能夠是一個文件或一個目錄。 返回的對象是一個 fs.FSWatcher
。
第二個參數是可選的。 若是提供的 options
是一個字符串,則它指定了 encoding
。 不然 options
應該以一個對象傳入。
監聽器回調有兩個參數 (eventType, filename)
。 eventType
能夠是 'rename'
或 'change'
,filename
是觸發事件的文件的名稱。
注意,在大多數平臺,當一個文件出現或消失在一個目錄裏時,'rename'
會被觸發。
還須要注意,監聽器回調是綁定在由 fs.FSWatcher
觸發的 'change'
事件上,但它跟 eventType
的 'change'
值不是同一個東西。
fs.watch
API 不是 100% 跨平臺一致的,且在某些狀況下不可用。
遞歸選項只支持 macOS 和 Windows。
該特性依賴於底層操做系統提供的一種方法來通知文件系統的變化。
inotify
。kqueue
。kqueue
,對目錄使用 FSEvents
。event ports
。ReadDirectoryChangesW
。AHAFS
必須是啓動的。若是底層功能因某些緣由不可用,則 fs.watch
也沒法正常工做。 例如,當使用虛擬化軟件如 Vagrant、Docker 等時,在網絡文件系統(NFS、SMB 等)或主文件系統中監視文件或目錄多是不可靠的。
您仍然可使用基於stat輪詢的fs.watchFile()
,可是這種方法更慢,可靠性也更低。
在 Linux 或 macOS 系統中,fs.watch()
解析路徑到一個索引節點,並監視該索引節點。 若是監視的路徑被刪除或重建,則它會被分配一個新的索引節點。 監視器會發出一個刪除事件,但會繼續監視原始的索引節點。 新建的索引節點的事件不會被觸發。 這是正常的行爲。
In AIX, save and close of a file being watched causes two notifications - one for adding new content, and one for truncation. Moreover, save and close operations on some platforms cause inode changes that force watch operations to become invalid and ineffective. AIX retains inode for the lifetime of a file, that way though this is different from Linux / macOS, this improves the usability of file watching. This is expected behavior.
回調中提供的 filename
參數僅在 Linux、macOS、Windows、以及 AIX 系統上支持。 即便在支持的平臺中,filename
也不能保證提供。 所以,不要覺得 filename
參數老是在回調中提供,若是它是空的,須要有必定的後備邏輯。
fs.watch('somedir', (eventType, filename) => { console.log(`事件類型是: ${eventType}`); if (filename) { console.log(`提供的文件名: ${filename}`); } else { console.log('未提供文件名'); } });
filename
<string> | <Buffer> | <URL>options
<Object>
listener
<Function>監視 filename
的變化。 回調 listener
會在每次訪問文件時被調用。
options
參數可被省略。 若是提供的話,它應該是一個對象。 options
對象可能包含一個名爲 persistent
的布爾值,代表當文件正在被監視時,進程是否應該繼續運行。 options
對象能夠指定一個 interval
屬性,表示目標應該每隔多少毫秒被輪詢。 默認值爲 { persistent: true, interval: 5007 }
。
listener
有兩個參數,當前的狀態對象和之前的狀態對象:
fs.watchFile('message.text', (curr, prev) => { console.log(`the current mtime is: ${curr.mtime}`); console.log(`the previous mtime was: ${prev.mtime}`); });
These stat objects are instances of fs.Stat
. 這裏的狀態對象是 fs.Stat
實例。
若是你想在文件被修改而不僅是訪問時獲得通知,則須要比較 curr.mtime
和 prev.mtime
。
注意:當一個 fs.watchFile
的運行結果是一個 ENOENT
錯誤時,它會調用監聽器一次,且將全部字段置零(或將日期設爲 Unix 紀元)。 在 Windows 中,blksize
和 blocks
字段會是 undefined
而不是零。 若是文件是在那以後建立的,則監聽器會被再次調用,且帶上最新的狀態對象。 這是在 v0.10 版以後在功能上的變化。
注意:fs.watch()
比 fs.watchFile
和 fs.unwatchFile
更高效。 可能的話,應該使用 fs.watch
而不是 fs.watchFile
和 fs.unwatchFile
。
fd
<integer>buffer
<Buffer> | <Uint8Array>offset
<integer>length
<integer>position
<integer>callback
<Function>寫入 buffer
到 fd
指定的文件。
offset
決定 buffer 中被寫入的部分,length
是一個整數,指定要寫入的字節數。
position
指向從文件開始寫入數據的位置的偏移量。 若是 typeof position !== 'number'
,則數據從當前位置寫入。詳見 pwrite(2)。
回調有三個參數 (err, bytesWritten, buffer)
,其中 bytesWritten
指定從 buffer
寫入了多少字節。
If this method is invoked as its util.promisify()
ed version, it returns a Promise for an object with bytesWritten
and buffer
properties.
注意,屢次對同一文件使用 fs.write
且不等待回調,是不安全的。 對於這種狀況,強烈推薦使用 fs.createWriteStream
。
在 Linux 上,當文件以追加模式打開時,指定位置的寫入是不起做用的。 內核會忽略位置參數,並老是將數據追加到文件的末尾。
fd
<integer>string
<string>position
<integer>encoding
<string>callback
<Function>寫入 string
到 fd
指定的文件。 若是 string
不是一個字符串,則該值將被強制轉換爲一個字符串。
position
指向從文件開始寫入數據的位置的偏移量。 若是 typeof position !== 'number'
,則數據從當前位置寫入。詳見 pwrite(2)。
encoding
是指望的字符串編碼。
回調有三個參數 (err, written, string)
,其中 written
指定傳入的字符串被寫入多少字節。 注意,寫入的字節與字符串的字符是不一樣的。詳見 Buffer.byteLength
。
不一樣於寫入 buffer
,該方法整個字符串必須被寫入。 不能指定子字符串。 這是由於結果數據的字節偏移量可能與字符串的偏移量不一樣。
注意,屢次對同一文件使用 fs.write
且不等待回調,是不安全的。 對於這種狀況,強烈推薦使用 fs.createWriteStream
。
在 Linux 上,當文件以追加模式打開時,指定位置的寫入是不起做用的。 內核會忽略位置參數,並老是將數據追加到文件的末尾。
file
<string> | <Buffer> | <integer> 文件名或文件描述符data
<string> | <Buffer> | <Uint8Array>options
<Object> | <string>
callback
<Function>異步地寫入數據到文件,若是文件已經存在,則替代文件。 data
能夠是一個字符串或一個 buffer。
若是 data
是一個 buffer,則忽略 encoding
選項。它默認爲 'utf8'
。
例子:
fs.writeFile('message.txt', 'Hello Node.js', (err) => { if (err) throw err; console.log('The file has been saved!'); });
若是 options
是一個字符串,則它指定了字符編碼。例如:
fs.writeFile('message.txt', 'Hello Node.js', 'utf8', callback);
任何指定的文件描述符必須支持寫入。
注意,屢次對同一文件使用 fs.writeFile
且不等待回調,是不安全的。 對於這種狀況,強烈推薦使用 fs.createWriteStream
。
注意:若是 file
指定爲一個文件描述符,則它不會被自動關閉。
file
<string> | <Buffer> | <integer> 文件名或文件描述符data
<string> | <Buffer> | <Uint8Array>options
<Object> | <string>
fs.writeFile()
的同步版本。返回 undefined
。
fs.write()
的同步版本。返回寫入的字節數。