The fs.truncateSync() method is used to synchronously change the size of the file i.e. either increase or decrease the file size. It changes the length of the file at the path by len bytes. If len is shorter than the file’s current length, the file is truncated to that length. If it is greater than the file length, it is padded by appending null bytes (x00) until len is reached.
fs.truncateSync( path, len )
Parameters: This method accept two parameters as mentioned above and described below:
- path: It is a string, Buffer, URL that denotes the path of the file that has to be trruncated.
- len: It is an integer which specifies the length of the file after which the file will be truncated. It is an optional parameter. The default value is 0, which means that the whole file would be truncated.
Below examples illustrate the fs.truncateSync() method in Node.js:
Contents of file before truncate: This is an example file for the truncateSync() method. Contents of file after truncate: This is an example
Contents of file before truncate: This is an example file for the truncateSync() method. Contents of file after truncate:
- Nodejs | Automatic restart NodeJs server with nodemon
- Nodejs | DNS
- Nodejs | Jimp
- Web-Socket in NodeJS
- Nodejs | DNS | setServers()
- Encrypting Data in NodeJS
- Nodejs | GM drawBezier() Function
- Nodejs | GM drawCircle() Function
- Nodejs | GM drawEllipse() Function
- Nodejs | GM drawPolygon() Function
- Difference between NodeJS and AngularJS
- Nodejs | GM randomThreshold() Function
- Nodejs | GM recolor() Function
- Nodejs | GM implode() Function
- Nodejs | Web Crawling using Cheerio
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.