Node.js stats.dev 属性
stats.dev属性是 fs.Stats 类的内置应用程序编程接口,用于获取文件所在设备的数字(数字/大整数)标识。
句法:
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