Node.js stats.blocks Property from fs.Stats Class

The stats.blocks property is an inbuilt application programming interface of the fs.Stats class is used to get the number of blocks allocated for the file.

Syntax:

stats.blocks;

Return Value: It returns a number or BigInt value that represents the number of blocks allocated for the file.

Below examples illustrate the use of stats.blocks property in Node.js:

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the
// stats.blocks property
  
// Accessing fs module
const fs = require('fs');
  
// Calling fs.Stats stats.blocks
// using stat
fs.stat('./', (err, stats) => {
    if (err) throw err;
  
    // The number of blocks allocated
    // for the file 
    console.log("using stat: " 
                + stats.blocks);
});
  
// Using lstat
fs.lstat('./', (err, stats) => {
    if (err) throw err;
  
    // The number of blocks allocated
    // for the file 
    console.log("using lstat: " 
                + stats.blocks);
});

chevron_right


Output:

using stat: 8
using lstat: 8

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the
// stats.blocks property
  
// Accessing fs module
const fs = require('fs').promises;
  
// Calling fs.Stats stats.blocks
(async () => {
    const stats = await fs.stat('./filename.txt');
  
    // The number of blocks allocated
    // for the file
    console.log("using stat synchronous: "
                + stats.blocks);
})().catch(console.error)

chevron_right


Output:

using stat synchronous: 8

Note: The above program will compile and run by using the node filename.js command and use the file_path correctly.

Reference: https://nodejs.org/api/fs.html#fs_stats_blocks




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.