当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Node.js fs.stat()用法及代码示例


fs.stat()方法用于返回有关给定文件或目录的信息。它返回一个fs.Stat对象,该对象具有几个属性和方法来获取有关文件或目录的详细信息。

用法:

fs.stat( path, options, callback )

参数:此方法接受上述和以下所述的三个参数:


  • path:它包含必须检查的文件或目录的路径。它可以是字符串,缓冲区或URL。
  • options:该对象可用于指定将影响输出的可选参数。它具有一个可选参数:
    • bigint:它是一个布尔值,它指定fs.Stats对象中返回的数值是否为bigint。默认值为false。
  • callback:执行该方法时将调用该函数。
    • err:如果该方法会引发错误
    • stats:Stats对象包含文件路径的详细信息。

以下示例说明了Node.js中的fs.stat()方法:

范例1:本示例使用fs.stat()方法获取路径的详细信息。

// Node.js program to demonstrate the 
// fs.stat() method 
  
// Import the filesystem module 
const fs = require('fs'); 
  
// Getting information for a file 
fs.stat("example_file.txt", (error, stats) => { 
  if (error) { 
    console.log(error); 
  } 
  else { 
    console.log("Stats object for:example_file.txt"); 
    console.log(stats); 
  
    // Using methods of the Stats object 
    console.log("Path is file:", stats.isFile()); 
    console.log("Path is directory:", stats.isDirectory()); 
  } 
}); 
  
// Getting information for a directory 
fs.stat("example_directory.txt", (error, stats) => { 
  if (error) { 
    console.log(error); 
  } 
  else { 
    console.log("Stats object for:example_directory.txt"); 
    console.log(stats); 
  
    // Using methods of the Stats object 
    console.log("Path is file:", stats.isFile()); 
    console.log("Path is directory:", stats.isDirectory()); 
  } 
});

输出:

Stats object for:example_file.txt
Stats {
  dev:2269,
  mode:33188,
  nlink:1,
  uid:1000,
  gid:1000,
  rdev:0,
  blksize:4096,
  ino:271,
  size:0,
  blocks:0,
  atimeMs:1582871562365.894,
  mtimeMs:1582871556897.5554,
  ctimeMs:1582871556897.5554,
  birthtimeMs:1582871556897.5554,
  atime:2020-02-28T06:32:42.366Z,
  mtime:2020-02-28T06:32:36.898Z,
  ctime:2020-02-28T06:32:36.898Z,
  birthtime:2020-02-28T06:32:36.898Z }
Path is file:true
Path is directory:false
Stats object for:example_directory.txt
Stats {
  dev:2269,
  mode:33188,
  nlink:1,
  uid:1000,
  gid:1000,
  rdev:0,
  blksize:4096,
  ino:270,
  size:0,
  blocks:0,
  atimeMs:1582871562357.8936,
  mtimeMs:1582871553413.3396,
  ctimeMs:1582871553417.3398,
  birthtimeMs:1582871553413.3396,
  atime:2020-02-28T06:32:42.358Z,
  mtime:2020-02-28T06:32:33.413Z,
  ctime:2020-02-28T06:32:33.417Z,
  birthtime:2020-02-28T06:32:33.413Z }
Path is file:true
Path is directory:false

范例2:本示例使用fs.stat()方法使用bigint选项获取文件的详细信息。

// Node.js program to demonstrate the 
// fs.stat() method 
  
// Import the filesystem module 
const fs = require('fs'); 
  
fs.stat("example_file.txt", (error, stats) => { 
  console.log(stats); 
}); 
  
// Using the bigint option to return 
// the values in big integer format 
fs.stat("example_file.txt", { bigint:true }, (error, stats) => { 
  console.log(stats); 
});

输出:

Stats {
  dev:2269,
  mode:33188,
  nlink:1,
  uid:1000,
  gid:1000,
  rdev:0,
  blksize:4096,
  ino:271,
  size:0,
  blocks:0,
  atimeMs:1582871562365.894,
  mtimeMs:1582871556897.5554,
  ctimeMs:1582871556897.5554,
  birthtimeMs:1582871556897.5554,
  atime:2020-02-28T06:32:42.366Z,
  mtime:2020-02-28T06:32:36.898Z,
  ctime:2020-02-28T06:32:36.898Z,
  birthtime:2020-02-28T06:32:36.898Z }
Stats {
  dev:2269n,
  mode:33188n,
  nlink:1n,
  uid:1000n,
  gid:1000n,
  rdev:0n,
  blksize:4096n,
  ino:271n,
  size:0n,
  blocks:0n,
  atimeMs:1582871562365n,
  mtimeMs:1582871556897n,
  ctimeMs:1582871556897n,
  birthtimeMs:1582871556897n,
  atime:2020-02-28T06:32:42.365Z,
  mtime:2020-02-28T06:32:36.897Z,
  ctime:2020-02-28T06:32:36.897Z,
  birthtime:2020-02-28T06:32:36.897Z }

参考: https://nodejs.org/api/fs.html#fs_fs_stat_path_options_callback



相关用法


注:本文由纯净天空筛选整理自sayantanm19大神的英文原创作品 Node.js | fs.stat() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。