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 关注
- 1381 浏览
添加回答
举报