fs.writeFile(file, data[, options], callback)
Asynchronously writes data to a file, replacing the file if it already exists. data
can be a string or a buffer.
The encoding
option is ignored if data
is a buffer. It defaults to 'utf8'
.
Example:
fs.writeFile('message.txt', 'Hello Node.js', (err) => { if (err) throw err; console.log('It\'s saved!'); });
If options
is a string, then it specifies the encoding. Example:
fs.writeFile('message.txt', 'Hello Node.js', 'utf8', callback);
Any specified file descriptor has to support writing.
Note that it is unsafe to use fs.writeFile
multiple times on the same file without waiting for the callback. For this scenario, fs.createWriteStream
is strongly recommended.
Note: Specified file descriptors will not be closed automatically.
Please login to continue.