メソッド説明:
fs.writefile()の同期バージョン。
文法:
コードコピーは次のとおりです。
fs.writefilesync(filename、data、[options])
この方法はFSモジュールに属しているため、使用前にFSモジュールを導入する必要があります(var fs = require( "fs"))
パラメーターを受信:
ファイル名(文字列)ファイル名
データ(文字列|バッファー)文字列またはバッファデータを作成できるコンテンツ。
オプション(オブジェクト)オプションアレイオブジェクトを含む:
cording(文字列)オプションの値、デフォルトの 'utf8'、データバッファーの場合、この値は無視する必要があります。
・モード(number)ファイルの読み取りおよび書き込み許可、デフォルト値438
・フラグ(文字列)デフォルト値 '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)){
新しいTypeError(「悪い引数」)を投げる;
}
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 writed = 0;
var length = data.length;
var position = /a/.test(flag)?ヌル:0;
試す {
while(書かれた<長さ){
writed += fs.writesync(fd、data、writen、onegh -writed、position);
位置 +=書かれた;
}
} ついに {
fs.CloseSync(FD);
}
};