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
相關用法
- Node.js GM charcoal()用法及代碼示例
- Node.js GM blur()用法及代碼示例
- Node.js GM sharpen()用法及代碼示例
- Node.js GM drawLine()用法及代碼示例
- Node.js GM drawArc()用法及代碼示例
- Node.js GM drawPolyline()用法及代碼示例
- Node.js GM drawBezier()用法及代碼示例
- Node.js GM drawCircle()用法及代碼示例
注:本文由純淨天空篩選整理自anwesha0107大神的英文原創作品 Node.js stats.mode Property from fs.Stats Class。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。