![]() |
VOOZH | about |
The fs.filehandle.truncate() method is an inbuilt application programming interface of class fs.filehandle within File System module which is used to truncate the particular file object and only that much byte will be retained which passed as an integer in truncate() method.
Syntax:
const filehandle.truncate(len)
Parameter: This method takes the length of bytes till which this file has to be truncated.
Return value: This method returns an pending promise which contains no value in it.
Below programs illustrates the use of fs.filehandle.truncate() method in Node.js:
Example 1: Filename: index.js
Run index.js file using the following command:
node index.js
Output:
data before operation: ABCDEFGHIJK data after operation: ABCDE
Example 2: Filename: index.js
Directory structure before running the program:
Directory structure after running the program:
Run index.js file using the following command:
node index.js
Output:
content of file before operation: This is a file containing a collection of books. content of file after operation: This
NOTE: The above program will not run on online JavaScript and script editor.
Reference: https://nodejs.org/dist/latest-v12.x/docs/api/fs.html#fs_filehandle_truncate_len