path
{string|Buffer|URL}options
{Object}bigint
{boolean} 返回的 [fs.Stats
] 对象中的数值是否为bigint
型。 默认值:false
。
callback
{Function}err
{Error}stats
{fs.Stats}
异步的 stat(2)。
回调有两个参数 (err, stats)
,其中 stats
是 [fs.Stats
] 对象。
如果发生错误,则 err.code
会是[常见的系统错误][Common System Errors]之一。
不建议在调用 fs.open()
、fs.readFile()
或 fs.writeFile()
之前使用 fs.stat()
检查文件的存在性。
而是应该直接地打开、读取或写入文件,如果文件不可用,则处理引发的错误。
若要只检查文件是否存在,但没有更多的操作,则建议使用 [fs.access()
]。
例如,给定以下的目录结构:
- 目录
-- 文件.txt
- 文件.js
以下程序会检查给定路径的文件属性:
const fs = require('fs');
const pathsToCheck = ['./目录', './目录/文件.txt'];
for (let i = 0; i < pathsToCheck.length; i++) {
fs.stat(pathsToCheck[i], function(err, stats) {
console.log(stats.isDirectory());
console.log(stats);
});
}
结果的输出会类似于:
true
Stats {
dev: 16777220,
mode: 16877,
nlink: 3,
uid: 501,
gid: 20,
rdev: 0,
blksize: 4096,
ino: 14214262,
size: 96,
blocks: 0,
atimeMs: 1561174653071.963,
mtimeMs: 1561174614583.3518,
ctimeMs: 1561174626623.5366,
birthtimeMs: 1561174126937.2893,
atime: 2019-06-22T03:37:33.072Z,
mtime: 2019-06-22T03:36:54.583Z,
ctime: 2019-06-22T03:37:06.624Z,
birthtime: 2019-06-22T03:28:46.937Z
}
false
Stats {
dev: 16777220,
mode: 33188,
nlink: 1,
uid: 501,
gid: 20,
rdev: 0,
blksize: 4096,
ino: 14214074,
size: 8,
blocks: 8,
atimeMs: 1561174616618.8555,
mtimeMs: 1561174614584,
ctimeMs: 1561174614583.8145,
birthtimeMs: 1561174007710.7478,
atime: 2019-06-22T03:36:56.619Z,
mtime: 2019-06-22T03:36:54.584Z,
ctime: 2019-06-22T03:36:54.584Z,
birthtime: 2019-06-22T03:26:47.711Z
}