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


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


fs.statSync()方法用于异步返回有关给定文件路径的信息。返回的fs.Stat对象具有多个字段和方法,以获取有关文件的更多详细信息。

用法:

fs.statSync( path, options )

参数:该方法接受上述和以下所述的两个参数:



  • path:它包含必须检查的文件的路径。它可以是字符串,缓冲区或URL。
  • options:该对象可用于指定将影响输出的可选参数。它具有一个可选参数:
    • bigint:它是一个布尔值,它指定fs.Stats对象中返回的数值是否为bigint。默认值为false。

返回值:它返回一个Stats对象,其中包含文件路径的详细信息。

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

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

// Node.js program to demonstrate the 
// fs.statSync() method 
  
// Import the filesystem module 
const fs = require('fs'); 
  
// Getting information for a file 
statsObj = fs.statSync("test_file.txt"); 
  
console.log(statsObj);  
console.log("Path is file:", statsObj.isFile()); 
console.log("Path is directory:", statsObj.isDirectory()); 
  
// Getting information for a directory 
statsObj = fs.statSync("test_directory"); 
  
console.log(statsObj); 
console.log("Path is file:", statsObj.isFile()); 
console.log("Path is directory:", statsObj.isDirectory());

输出:

Stats {
  dev:3229478529,
  mode:33206,
  nlink:1,
  uid:0,
  gid:0,
  rdev:0,
  blksize:4096,
  ino:1970324837039946,
  size:0,
  blocks:0,
  atimeMs:1582306776282,
  mtimeMs:1582482953967,
  ctimeMs:1582482953968.2532,
  birthtimeMs:1582306776282.142,
  atime:2020-02-21T17:39:36.282Z,
  mtime:2020-02-23T18:35:53.967Z,
  ctime:2020-02-23T18:35:53.968Z,
  birthtime:2020-02-21T17:39:36.282Z
}
Path is file:true
Path is directory:false
Stats {
  dev:3229478529,
  mode:16822,
  nlink:1,
  uid:0,
  gid:0,
  rdev:0,
  blksize:4096,
  ino:562949953486669,
  size:0,
  blocks:0,
  atimeMs:1582482965037.8445,
  mtimeMs:1581074249467.7114,
  ctimeMs:1582482964979.8303,
  birthtimeMs:1582306776288.1958,
  atime:2020-02-23T18:36:05.038Z,
  mtime:2020-02-07T11:17:29.468Z,
  ctime:2020-02-23T18:36:04.980Z,
  birthtime:2020-02-21T17:39:36.288Z
}
Path is file:false
Path is directory:true

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

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

输出:

Stats {
  dev:3229478529,
  mode:33206,
  nlink:1,
  uid:0,
  gid:0,
  rdev:0,
  blksize:4096,
  ino:1970324837039946,
  size:0,
  blocks:0,
  atimeMs:1582306776282,
  mtimeMs:1582482953967,
  ctimeMs:1582482953968.2532,
  birthtimeMs:1582306776282.142,
  atime:2020-02-21T17:39:36.282Z,
  mtime:2020-02-23T18:35:53.967Z,
  ctime:2020-02-23T18:35:53.968Z,
  birthtime:2020-02-21T17:39:36.282Z
}
BigIntStats {
  dev:3229478529n,
  mode:33206n,
  nlink:1n,
  uid:0n,
  gid:0n,
  rdev:0n,
  blksize:4096n,
  ino:1970324837039946n,
  size:0n,
  blocks:0n,
  atimeMs:1582306776282n,
  mtimeMs:1582482953967n,
  ctimeMs:1582482953968n,
  birthtimeMs:1582306776282n,
  atimeNs:1582306776282000000n,
  mtimeNs:1582482953967000000n,
  ctimeNs:1582482953968253100n,
  birthtimeNs:1582306776282142200n,
  atime:2020-02-21T17:39:36.282Z,
  mtime:2020-02-23T18:35:53.967Z,
  ctime:2020-02-23T18:35:53.968Z,
  birthtime:2020-02-21T17:39:36.282Z
}

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




相关用法


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