Method Description:
File content interception operation.
Grammar:
Copy Code code as follows:
Fs.truncate (Path, Len, [Callback (ERR)])
Because this method belongs to the FS module, it is necessary to introduce FS module (VAR fs= require ("FS") before use.
Receive parameters:
Path file paths
Len truncate length, leaving only the characters in the length of the character, and the excess portion will be cleared.
Callback callback, passing an exception parameter err
Example:
Copy Code code as follows:
var fs = require (' FS ');
Fs.truncate (' 126.txt ', 2, function (err) {
if (err) {
throw err;
}
Console.log (' File content truncated successfully ');
})
Source:
Copy Code code as follows:
Fs.truncate = function (path, Len, callback) {
if (Util.isnumber (path)) {
Legacy
return fs.ftruncate (Path, Len, callback);
}
if (Util.isfunction (len)) {
callback = Len;
len = 0;
else if (util.isundefined (len)) {
len = 0;
}
callback = Maybecallback (callback);
Fs.open (Path, ' r+ ', function (er, FD) {
if (ER) return callback (ER);
Binding.ftruncate (FD, Len, function (er) {
Fs.close (FD, function (er2) {
Callback (er | | er2);
});
});
});
};