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


Node.js stats.gid屬性用法及代碼示例

stats.gid屬性是fs.Stats類的內置應用程序編程接口,用於獲取文件所屬組的數字(數字/bigint)標識。

用法:

stats.gid;

返回值:它返回一個數字或BigInt值,表示擁有該文件的組的標識。

以下示例說明了Node.js中stats.gid屬性的使用:

範例1:



// Node.js program to demonstrate the    
// stats.gid property 
  
// Accessing fs module 
const fs = require('fs'); 
  
// Calling fs.Stats stats.gid 
// for directory using stat 
fs.stat('./', (err, stats) => { 
    if (err) throw err; 
    console.log("using stat:numeric "
        + "identity of the group is "
        + stats.gid); 
}); 
  
// Using lstat 
fs.lstat('./', (err, stats) => { 
    if (err) throw err; 
    console.log("using lstat:numeric "
        + "identity of the group is "
        + stats.gid); 
}); 
  
// For file 
// Using stat 
fs.stat('./filename.txt', (err, stats) => { 
    if (err) throw err; 
    console.log("using stat:numeric "
        + "identity of the group is "
        + stats.gid); 
}); 
  
// Using lstat 
fs.lstat('./filename.txt', (err, stats) => { 
    if (err) throw err; 
    console.log("using lstat:numeric "
        + "identity of the group is "
        + stats.gid); 
});

輸出:

using stat:numeric identity of the group is  5687
using lstat:numeric identity of the group is  5687
using stat:numeric identity of the group is  5687
using lstat:numeric identity of the group is  5687

範例2:

// Node.js program to demonstrate the    
// stats.gid property 
  
// Accessing fs module 
const fs = require('fs').promises; 
  
// Calling fs.Stats stats.gid 
(async () => { 
    const stats = await fs.stat('./filename.txt'); 
    console.log("using stat synchronous:numeric"
        + " identity of the group is  " + stats.gid); 
})().catch(console.error)

輸出:

(node:15204) ExperimentalWarning:The fs.promises API 
is experimental 
using stat synchronous:numeric identity of the group is 5687

注意:上麵的程序將通過使用node filename.js命令並正確使用file_path。此API將在POSIX係統上正常工作。在其他係統(如WINDOWS)中,它將返回0。

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




相關用法


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