Truncating Files

Truncating a file means reducing the size of the file by setting the end to a smaller value than the current value. You may want to truncate files that grow continuously but do not contain critical data (for example, a temporary log). To truncate a file, you use one of the following fs calls and pass in the number of bytes you want the file to contain when the truncation completes:

fs.truncate(path, len, callback)
fs.truncateSync(path, len)

The truncateSync(path) function returns true or false, depending on whether the file was successfully truncated. On the other hand, an asynchronous truncate() call passes an error value to the callback function if an error is encountered when truncating the file.

The following code snippet illustrates the process of truncating a file named log.txt to zero bytes:

fs.truncate("new.txt", function(err){
  console.log(err ? "File Truncate Failed" :  "File Truncated");
});

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.162.105