當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Node.js fs.fstat()用法及代碼示例

fs.fstat()方法用於返回有關給定文件描述符的信息。返回的fs.Stat對象具有多個字段和方法,以獲取有關文件的更多詳細信息。

用法:

fs.fstat( fd, options, callback )

參數:此方法接受上述和以下所述的三個參數:



  • fd:它是一個整數,代表該方法使用的文件描述符。
  • options:該對象可用於指定將影響輸出的可選參數。它具有一個可選參數:
    • bigint:它是一個布爾值,它指定fs.Stats對象中返回的數值是否為bigint。默認值為false。
  • callback:執行該方法時將調用該函數。
    • err:如果該方法會引發錯誤
    • Stats:Stats對象包含文件路徑的詳細信息。

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

範例1:本示例使用fs.fstat()方法獲取文件和目錄的詳細信息。

// Node.js program to demonstrate the 
// fs.fstat() method 
  
// Import the filesystem module 
const fs = require('fs'); 
  
// Define the file descriptor for a file 
let file_fd = fs.openSync('example_file.txt', 'r'); 
  
// Getting information for a file 
fs.fstat(file_fd, (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()); 
  } 
}); 
  
// Define the file descriptor for a folder 
let dir_fd = fs.openSync('example_directory', 'r'); 
  
// Getting information for a directory 
fs.fstat(dir_fd, (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:3229478529,
  mode:33206,
  nlink:1,
  uid:0,
  gid:0,
  rdev:0,
  blksize:4096,
  ino:281474976780635,
  size:0,
  blocks:0,
  atimeMs:1584389463707.251,
  mtimeMs:1582209885466.6848,
  ctimeMs:1582209885466.6848,
  birthtimeMs:1584389463707.251,
  atime:2020-03-16T20:11:03.707Z,
  mtime:2020-02-20T14:44:45.467Z,
  ctime:2020-02-20T14:44:45.467Z,
  birthtime:2020-03-16T20:11:03.707Z
}
Path is file:true
Path is directory:false
Stats object for:example_directory.txt
Stats {
  dev:3229478529,
  mode:16822,
  nlink:1,
  uid:0,
  gid:0,
  rdev:0,
  blksize:4096,
  ino:281474976780638,
  size:0,
  blocks:0,
  atimeMs:1584429828080.8872,
  mtimeMs:1581074249467.7114,
  ctimeMs:1584389463715.2507,
  birthtimeMs:1584389463715.2507,
  atime:2020-03-17T07:23:48.081Z,
  mtime:2020-02-07T11:17:29.468Z,
  ctime:2020-03-16T20:11:03.715Z,
  birthtime:2020-03-16T20:11:03.715Z
}
Path is file:false
Path is directory:true

範例2:本示例使用fs.fstat()方法獲取帶有bigint選項和不帶有bigint選項的文件的詳細信息。

// Node.js program to demonstrate the 
// fs.fstat() method 
  
// Import the filesystem module 
const fs = require('fs'); 
  
// Define the file descriptor for a file 
let file_fd = fs.openSync('example_file.txt', 'r'); 
  
fs.fstat(file_fd, (error, stats) => { 
  console.log(stats); 
}); 
  
// Using the bigint option to return 
// the values in big integer format 
fs.fstat(file_fd, { bigint:true }, (error, stats) => { 
  console.log(stats); 
});

輸出:

Stats {
  dev:3229478529,
  mode:33206,
  nlink:1,
  uid:0,
  gid:0,
  rdev:0,
  blksize:4096,
  ino:281474976780635,
  size:0,
  blocks:0,
  atimeMs:1584389463707.251,
  mtimeMs:1582209885466.6848,
  ctimeMs:1582209885466.6848,
  birthtimeMs:1584389463707.251,
  atime:2020-03-16T20:11:03.707Z,
  mtime:2020-02-20T14:44:45.467Z,
  ctime:2020-02-20T14:44:45.467Z,
  birthtime:2020-03-16T20:11:03.707Z
}
BigIntStats {
  dev:3229478529n,
  mode:33206n,
  nlink:1n,
  uid:0n,
  gid:0n,
  rdev:0n,
  blksize:4096n,
  ino:281474976780635n,
  size:0n,
  blocks:0n,
  atimeMs:1584389463707n,
  mtimeMs:1582209885466n,
  ctimeMs:1582209885466n,
  birthtimeMs:1584389463707n,
  atimeNs:1584389463707251000n,
  mtimeNs:1582209885466684900n,
  ctimeNs:1582209885466684900n,
  birthtimeNs:1584389463707251000n,
  atime:2020-03-16T20:11:03.707Z,
  mtime:2020-02-20T14:44:45.466Z,
  ctime:2020-02-20T14:44:45.466Z,
  birthtime:2020-03-16T20:11:03.707Z
}

參考: https://nodejs.org/api/fs.html#fs_fs_fstat_fd_options_callback




相關用法


注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 Node.js | fs.fstat() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。