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


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


fs.fstatSync()方法用于同步返回有关给定文件描述符的信息。 fs.Stat对象返回几个字段和方法,以获取有关文件的更多详细信息。

用法:

fstatSync( fd, options )

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



  • fd:它是一个整数值,代表该方法使用的文件描述符。
  • options:该对象可用于指定将影响输出的可选参数。它具有一个可选参数:
    • bigint:它是一个布尔值,它指定fs.Stats对象中返回的数值是否为bigint。默认值为false。

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

范例1:本示例使用fs.fstatSync()方法获取文件和目录的详细信息。

// Node.js program to demonstrate the 
// fs.fstatSync() method 
  
// Import the filesystem module 
const fs = require('fs'); 
  
const file_fd = fs.openSync('example_file.txt', 'r'); 
  
// Getting information for a file 
let fileStat = fs.fstatSync(file_fd); 
  
console.log("Stats object for:example_file.txt"); 
console.log(fileStat); 
  
// Using methods of the Stats object 
console.log("Path is file:", fileStat.isFile()); 
console.log("Path is directory:", fileStat.isDirectory()); 
  
const dir_fd = fs.openSync('example_directory', 'r'); 
  
// Getting information for a directory 
let dirStat = fs.fstatSync(dir_fd); 
  
console.log("Stats object for:example_directory"); 
console.log(dirStat); 
  
// Using methods of the Stats object 
console.log("Path is file:", dirStat.isFile()); 
console.log("Path is directory:", dirStat.isDirectory());

输出:

Stats object for:example_file.txt
Stats {
  dev:3229478529,
  mode:33206,
  nlink:1,
  uid:0,
  gid:0,
  rdev:0,
  blksize:4096,
  ino:281474976780786,
  size:0,
  blocks:0,
  atimeMs:1584961030299.117,
  mtimeMs:1582209885466.6848,
  ctimeMs:1582209885466.6848,
  birthtimeMs:1584961030299.117,
  atime:2020-03-23T10:57:10.299Z,
  mtime:2020-02-20T14:44:45.467Z,
  ctime:2020-02-20T14:44:45.467Z,
  birthtime:2020-03-23T10:57:10.299Z
}
Path is file:true
Path is directory:false
Stats object for:example_directory
Stats {
  dev:3229478529,
  mode:16822,
  nlink:1,
  uid:0,
  gid:0,
  rdev:0,
  blksize:4096,
  ino:281474976780789,
  size:0,
  blocks:0,
  atimeMs:1584961090214.9436,
  mtimeMs:1581074249467.7114,
  ctimeMs:1584961030324.12,
  birthtimeMs:1584961030324.12,
  atime:2020-03-23T10:58:10.215Z,
  mtime:2020-02-07T11:17:29.468Z,
  ctime:2020-03-23T10:57:10.324Z,
  birthtime:2020-03-23T10:57:10.324Z
}
Path is file:false
Path is directory:true

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

// Node.js program to demonstrate the 
// fs.fstatSync() method 
  
// Import the filesystem module 
const fs = require('fs'); 
  
const file_fd = fs.openSync('example_file.txt', 'r'); 
  
let fileStat = fs.fstatSync(file_fd); 
console.log(fileStat); 
  
// Using the bigint option to return 
// the values in big integer format 
let fileBigIntStat = fs.fstatSync(file_fd, { bigint:true }); 
console.log(fileBigIntStat);

输出:

Stats {
  dev:3229478529,
  mode:33206,
  nlink:1,
  uid:0,
  gid:0,
  rdev:0,
  blksize:4096,
  ino:281474976780786,
  size:0,
  blocks:0,
  atimeMs:1584961030299.117,
  mtimeMs:1582209885466.6848,
  ctimeMs:1582209885466.6848,
  birthtimeMs:1584961030299.117,
  atime:2020-03-23T10:57:10.299Z,
  mtime:2020-02-20T14:44:45.467Z,
  ctime:2020-02-20T14:44:45.467Z,
  birthtime:2020-03-23T10:57:10.299Z
}
BigIntStats {
  dev:3229478529n,
  mode:33206n,
  nlink:1n,
  uid:0n,
  gid:0n,
  rdev:0n,
  blksize:4096n,
  ino:281474976780786n,
  size:0n,
  blocks:0n,
  atimeMs:1584961030299n,
  mtimeMs:1582209885466n,
  ctimeMs:1582209885466n,
  birthtimeMs:1584961030299n,
  atimeNs:1584961030299117000n,
  mtimeNs:1582209885466684900n,
  ctimeNs:1582209885466684900n,
  birthtimeNs:1584961030299117000n,
  atime:2020-03-23T10:57:10.299Z,
  mtime:2020-02-20T14:44:45.466Z,
  ctime:2020-02-20T14:44:45.466Z,
  birthtime:2020-03-23T10:57:10.299Z
}

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




相关用法


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