Description of Fs.writefile method used in Node.js _node.js

Source: Internet
Author: User

Method Description:

Writes data to a file asynchronously, and the original content is replaced if the file already exists.


Copy Code code as follows:

Fs.writefile (filename, data, [options], [Callback (ERR)])

Because this method belongs to the FS module, it is necessary to introduce FS module (VAR fs= require ("FS") before use.

Receive parameters:

FileName (String) file name

Data (String | Buffer) will be written to the content that can be used to make string or buffer data.

Options (object) Option array object that contains:

· Encoding (string) optional value, default ' utf8′, which should be ignored when data makes buffer.

· Mode (number) file read and Write permission, default value 438

· Flag (String) Default value ' W '

callback {Function} callback, passing an exception parameter err.


Copy Code code as follows:

Fs.writefile (' Message.txt ', ' Hello Node ', function (err) {
if (err) throw err;
Console.log (' it\ ' s saved! ');


Copy Code code as follows:

Fs.writefile = function (path, data, options, callback) {
var callback = Maybecallback (Arguments[arguments.length-1]);
if (util.isfunction (options) | |!options) {
Options = {encoding: ' UTF8 ', mode:438/*=0666*/, flag: ' W '};
else if (util.isstring (options)) {
Options = {encoding:options, mode:438, flag: ' W '};
else if (!util.isobject (options)) {
throw new TypeError (' bad arguments ');
Assertencoding (options.encoding);
var flag = Options.flag | | ' W '; (path, Options.flag | | ' W ', Options.mode, function (Openerr, FD) {
if (Openerr) {
if (callback) callback (OPENERR);
} else {
var buffer = Util.isbuffer (data)? Data:new Buffer (' + data ',
options.encoding | | ' UTF8 ');
var position =/a/.test (flag)? null:0;
Writeall (fd, buffer, 0, buffer.length, position, callback);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.