Description of the fs. truncateSync method in node. js, node. jsfs
Method description:
Truncate () of the synchronous version.
Syntax:
Copy codeThe Code is as follows:
Fs. truncateSync (path, len)
Because this method belongs to the fs module, we need to introduce the fs module (var fs = require ("fs") before use "))
Receiving parameters:
Path file path
Len truncation length. Only characters within the length of this character are retained. The excess part is cleared.
Example:
Copy codeThe Code is as follows:
Var fs = require ('fs ');
Fs.truncateSync('126.txt ', 3 );
Source code:
Copy codeThe Code is as follows:
Fs. truncateSync = function (path, len ){
If (util. isNumber (path )){
// Legacy
Return fs. ftruncateSync (path, len );
}
If (util. isUndefined (len )){
Len = 0;
}
// Allow error to be thrown, but still close fd.
Var fd = fs. openSync (path, 'r + ');
Try {
Var ret = fs. ftruncateSync (fd, len );
} Finally {
Fs. closeSync (fd );
}
Return ret;
};