1 回答

TA貢獻358條經驗 獲得超213個贊
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';
? fs.open(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);
??? }
? });
};
這是writeFile的源碼,意思就是writeFile默認以'w'的方式打開,也就是打開文件的時候要先把文件內容置空。如果你想要追加打開,手動修改下配置。如:fs.writeFile('output.txt',data,{flag: 'w+'},function(err){}); ?
- 1 回答
- 1 關注
- 1426 瀏覽
添加回答
舉報