Method description:
The synchronous version of truncate() , file content interception operation.
grammar:
The code copy is as follows:
fs.truncateSync(path, len)
Since this method belongs to the fs module, it is necessary to introduce the fs module before use (var fs= require("fs") )
Receive parameters:
path file path
len truncates the length, retaining only the characters within the length of the character, and the excess will be cleared.
example:
The code copy is as follows:
var fs = require('fs');
fs.truncateSync('126.txt', 3);
Source code:
The code copy 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 return;
};