方法說明:
該方法以非同步的方式將 data 插入到檔案裡,如果檔案不存在會自動建立。 data可以是任意字串或快取。
文法:
fs.appendFile(filename, data, [options], callback)
由於方法屬於fs模組,使用前需要引入fs模組(var fs = require(“fs”) )
接收參數:
1. filename {String}
2. data {String | Buffer}
3. options {Object}
encoding {String | Null} default = ‘utf8′
mode {Number} default = 438 (aka 0666 in Octal)
flag {String} default = ‘a'
4. callback {Function}
範例:
var fs = require("fs");
fs.appendFile('message.txt', 'data to append', function (err) {
if (err) throw err;
console.log('The "data to append" was appended to file!');
});
原始碼:
fs.appendFile = function(path, data, options, callback_) {
var callback = maybeCallback(arguments[arguments.length - 1]);
if (util.isFunction(options) || !options) {
options = { encoding: 'utf8', mode: 438 /*=0666*/, flag: 'a' };
} else if (util.isString(options)) {
options = { encoding: options, mode: 438, flag: 'a' };
} else if (!util.isObject(options)) {
throw new TypeError('Bad arguments');
}
if (!options.flag)
options = util._extend({ flag: 'a' }, options);
fs.writeFile(path, data, options, callback);
};