node.js中的fs.appendFile方法使用說(shuō)明
方法說(shuō)明:
該方法以異步的方式將 data 插入到文件里,如果文件不存在會(huì)自動(dòng)創(chuàng)建。data可以是任意字符串或者緩存。
語(yǔ)法:
fs.appendFile(filename, data, [options], callback)
由于該方法屬于fs模塊,使用前需要引入fs模塊(var fs = require(“fs”) )
接收參數(shù):
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(chǎn)'
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);
};
- node.js用fs.rename強(qiáng)制重命名或移動(dòng)文件夾的方法
- node.js基于fs模塊對(duì)系統(tǒng)文件及目錄進(jìn)行讀寫(xiě)操作的方法詳解
- 基于node.js的fs核心模塊讀寫(xiě)文件操作(實(shí)例講解)
- node.js中fs.stat與fs.fstat的區(qū)別詳解
- 淺談Node.js:fs文件系統(tǒng)模塊
- node.js中的fs.chmodSync方法使用說(shuō)明
- node.js中的fs.chmod方法使用說(shuō)明
- node.js中的fs.appendFileSync方法使用說(shuō)明
- node.js中的fs.createWriteStream方法使用說(shuō)明
- node.js中的fs.futimesSync方法使用說(shuō)明
- node.js中fs文件系統(tǒng)目錄操作與文件信息操作
相關(guān)文章
Node.js中Path 模塊的介紹和使用示例小結(jié)
Node.js path 模塊提供了一些用于處理文件路徑的小工具,下面通過(guò)本文給大家介紹Node.js中Path 模塊的介紹和使用示例小結(jié),感興趣的朋友跟隨小編一起看看吧2024-05-05
node 標(biāo)準(zhǔn)輸入流和輸出流代碼實(shí)例
nodejs簡(jiǎn)單實(shí)現(xiàn)操作arduino
node.js利用socket.io實(shí)現(xiàn)多人在線匹配聯(lián)機(jī)五子棋
使用Puppeteer實(shí)現(xiàn)頁(yè)面遍歷的示例代碼

