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


node.js stats.dev用法及代碼示例

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

用法:

stats.dev;

參數:此屬性不接受任何參數。

返回值:它返回一個數字或BigInt值,該值表示文件所在設備的標識。

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



範例1:

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

輸出:

using stat:the numeric identity of the device is  891323748
using lstat:the numeric identity of the device is  891323748
using stat:the numeric identity of the device is  891323748
using lstat:the numeric identity of the device is  891323748

範例2:

// Node.js program to demonstrate the    
// stats.dev Property 
  
// Accessing fs module 
const fs = require('fs').promises; 
  
// Calling stats.dev property from 
// fs.Stats class 
(async () => { 
    const stats = await fs.stat( 
            './fs_stat_dev.txt'); 
  
    //using stat synchronous 
    console.log("The numeric identity "
        + "of the device is  " + stats.dev); 
})().catch(console.error)

輸出:

The numeric identity of the device is  891323748

注意:上麵的程序將通過使用以下命令進行編譯和運行node filename.js命令並正確使用file_path。

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




相關用法


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