fs.writeFile(file, data[, options], callback)
-
file<string> | <Buffer> | <URL> | <integer> 文件名或文件描述符¥
file<string> | <Buffer> | <URL> | <integer> filename or file descriptor -
data<string> | <Buffer> | <TypedArray> | <DataView> -
-
mode<integer> 默认值:0o666¥
mode<integer> Default:0o666 -
flag<string> 参见 支持文件系统flags。默认值:'w'。¥
flag<string> See support of file systemflags. Default:'w'. -
flush<boolean> 如果所有数据都成功写入文件,并且flush是true,则使用fs.fsync()来刷新数据。默认值:false。¥
flush<boolean> If all data is successfully written to the file, andflushistrue,fs.fsync()is used to flush the data. Default:false. -
signal<AbortSignal> 允许中止正在进行的 writeFile¥
signal<AbortSignal> allows aborting an in-progress writeFile
-
callback<Function>err<Error> | <AggregateError>
当 file 是文件名时,将数据异步地写入文件,如果文件已存在则替换该文件。data 可以是字符串或缓冲区。
¥When file is a filename, asynchronously writes data to the file, replacing the
file if it already exists. data can be a string or a buffer.
当 file 是文件描述符时,其行为类似于直接调用 fs.write()(推荐)。请参阅以下有关使用文件描述符的说明。
¥When file is a file descriptor, the behavior is similar to calling
fs.write() directly (which is recommended). See the notes below on using
a file descriptor.
如果 data 是缓冲区,则忽略 encoding 选项。
¥The encoding option is ignored if data is a buffer.
mode 选项仅影响新创建的文件。有关详细信息,请参阅 fs.open()。
¥The mode option only affects the newly created file. See fs.open()
for more details.
import { writeFile } from 'node:fs';
import { Buffer } from 'node:buffer';
const data = new Uint8Array(Buffer.from('Hello Node.js'));
writeFile('message.txt', data, (err) => {
if (err) throw err;
console.log('The file has been saved!');
}); 如果 options 是字符串,则它指定编码:
¥If options is a string, then it specifies the encoding:
import { writeFile } from 'node:fs';
writeFile('message.txt', 'Hello Node.js', 'utf8', callback); 在同一个文件上多次使用 fs.writeFile() 而不等待回调是不安全的。对于这种情况,建议使用 fs.createWriteStream()。
¥It is unsafe to use fs.writeFile() multiple times on the same file without
waiting for the callback. For this scenario, fs.createWriteStream() is
recommended.
与 fs.readFile 类似 - fs.writeFile 是一种便捷方法,它在内部执行多个 write 调用以写入传递给它的缓冲区。对于性能敏感的代码,则考虑使用 fs.createWriteStream()。
¥Similarly to fs.readFile - fs.writeFile is a convenience method that
performs multiple write calls internally to write the buffer passed to it.
For performance sensitive code consider using fs.createWriteStream().
可以使用 <AbortSignal> 取消 fs.writeFile()。取消是 "最大努力",可能仍有一些数据要写入。
¥It is possible to use an <AbortSignal> to cancel an fs.writeFile().
Cancelation is "best effort", and some amount of data is likely still
to be written.
import { writeFile } from 'node:fs';
import { Buffer } from 'node:buffer';
const controller = new AbortController();
const { signal } = controller;
const data = new Uint8Array(Buffer.from('Hello Node.js'));
writeFile('message.txt', data, { signal }, (err) => {
// When a request is aborted - the callback is called with an AbortError
});
// When the request should be aborted
controller.abort(); 中止正在进行的请求不会中止单个操作系统请求,而是中止内部缓冲的 fs.writeFile 执行。
¥Aborting an ongoing request does not abort individual operating
system requests but rather the internal buffering fs.writeFile performs.