This article mainly introduces node. fs. the usage of the truncateSync method is described in this article. truncateSync method description, syntax, receiving parameters, use instances and implementation source code. For more information, see
Method description:
Truncate () of the synchronous version.
Syntax:
The 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:
The Code is as follows:
Var fs = require ('fs ');
Fs.truncateSync('126.txt ', 3 );
Source code:
The 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;
};