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


Node.js stats.mode属性用法及代码示例


stats.mode属性是fs.Stats类的内置应用程序编程接口,用于获取文件类型和模式为bit-field。

用法:

stats.mode;

返回值:它返回一个数字或BigInt值,它表示描述文件类型和模式的bit-field。

以下示例说明了Node.js中stats.mode的使用:

范例1:



// Node.js program to demonstrate the    
// stats.mode property 
  
// Accessing fs module 
const fs = require('fs'); 
  
// Calling fs.Stats stats.mode for files 
// using stat 
fs.stat('./filename.txt', (err, stats) => { 
    if (err) throw err; 
    console.log("using stat:the type and "
        + "mode bit-field of the file is " 
        + stats.mode); 
}); 
  
// Using lstat 
fs.lstat('./filename.txt', (err, stats) => { 
    if (err) throw err; 
    console.log("using lstat:the type and "
        + "mode bit-field of the file is " 
        + stats.mode); 
}); 
  
// For directories 
// Using stat 
fs.stat('./', (err, stats) => { 
    if (err) throw err; 
    console.log("using stat:the type and "
        + "mode bit-field of the file is " 
        + stats.mode); 
}); 
  
// Using lstat 
fs.lstat('./', (err, stats) => { 
    if (err) throw err; 
    console.log("using lstat:the type and "
        + "mode bit-field of the file is " 
        + stats.mode); 
});

输出:

using stat:the type and mode bit-field of the file is  33206
using lstat:the type and mode bit-field of the file is  33206
using stat:the type and mode bit-field of the file is  16822
using lstat:the type and mode bit-field of the file is  16822

范例2:

// Node.js program to demonstrate the    
// stats.mode property 
  
// Accessing fs module 
const fs = require('fs').promises; 
   
// Calling fs.Stats stats.mode 
(async() => { 
    const stats = await fs.stat('./filename.txt'); 
    console.log("using stat synchronous:the type "
        + "and mode bit-field of the file is "
        + stats.mode); 
})().catch(console.error)

输出:

(node:9448) ExperimentalWarning:The fs.promises API
is experimental 
using stat synchronous:the type and mode bit-field 
of the file is  33206

注意:上面的程序将通过使用node filename.js命令并正确使用file_path。

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




相关用法


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