Method description:
Synchronized version of fs.writeFile() .
Grammar:
fs.writeFileSync(filename, data, [options])
Since this method belongs to the fs module, the fs module needs to be introduced before use (var fs= require(“fs”) )
Receive parameters:
filename (String) File name
data (String | Buffer) The content to be written can be string or buffer data.
options (Object) option array object, including:
· encoding (string) Optional value, default 'utf8', when data is buffer, the value should be ignored.
· mode (Number) File read and write permissions, default value 438
·flag (String) Default value ‘w’
Example:
fs.writeFileSync('message.txt', 'Hello Node');
Source code:
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 < length) {
Written = fs.writeSync(fd, data, written, length - written, position);
position = written;
}
} finally {
fs.closeSync(fd);
}
};
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn