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()
的同步版本。返回写入的字节数。