Node.js 文件操作与实用工具全解析
1. 提取文件权限信息
在处理文件时,有时我们需要获取文件的权限信息。这时可以使用 stat-mode 模块,它能接收 fs.stat() 等函数返回的 stat 对象,并让我们直接查询相关值。以下是一个示例代码:
var fs = require('fs');
var Mode = require('stat-mode');
fs.stat('./phoenix5a.png', function(err,stats) {
if (err) return console.log(err);
// get permissions
var mode = new Mode(stats);
console.log(mode.toString());
console.log('Group execute ' + mode.group.execute);
console.log('Others write ' + mode.others.write);
console.log('Owner read ' + mode.owner.read);
});
运行上述代码后,会输出文件的权限信息,例如:
-rw-rw-r--
Group execute false
Others write false
Owner read true
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



