node.js中的fs.writeFileSync方法使用说明_node.js
程序员文章站
2022-04-01 13:43:23
...
方法说明:
fs.writeFileSync(filename, data, [options])
fs.writeFileSync('message.txt', 'Hello Node');
fs.writeFileSync = function(path, data, options) {
if (!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';
var fd = fs.openSync(path, flag, options.mode);
if (!util.isBuffer(data)) {
data = new Buffer('' + data, options.encoding || 'utf8');
}
var written = 0;
var length = data.length;
var position = /a/.test(flag) ? null : 0;
try {
while (written written += fs.writeSync(fd, data, written, length - written, position);
position += written;
}
} finally {
fs.closeSync(fd);
}
};
同步版的fs.writeFile() 。
语法:
复制代码 代码如下:
fs.writeFileSync(filename, data, [options])
由于该方法属于fs模块,使用前需要引入fs模块(var fs= require(“fs”) )
接收参数:
filename (String) 文件名称
data (String | Buffer) 将要写入的内容,可以使字符串 或 buffer数据。
options (Object) option数组对象,包含:
· encoding (string) 可选值,默认 ‘utf8′,当data使buffer时,该值应该为 ignored。
· mode (Number) 文件读写权限,默认值 438
· flag (String) 默认值 ‘w'
例子:
复制代码 代码如下:
fs.writeFileSync('message.txt', 'Hello Node');
源码:
复制代码 代码如下:
fs.writeFileSync = function(path, data, options) {
if (!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';
var fd = fs.openSync(path, flag, options.mode);
if (!util.isBuffer(data)) {
data = new Buffer('' + data, options.encoding || 'utf8');
}
var written = 0;
var length = data.length;
var position = /a/.test(flag) ? null : 0;
try {
while (written written += fs.writeSync(fd, data, written, length - written, position);
position += written;
}
} finally {
fs.closeSync(fd);
}
};
上一篇: 编译PHP扩展的通用步骤
下一篇: PHP定时施行