fs.stat(path[, options], callback)
-
options
<Object>-
bigint
<boolean> 返回的 <fs.Stats> 对象中的数值是否应为bigint
。默认值:false
。¥
bigint
<boolean> Whether the numeric values in the returned <fs.Stats> object should bebigint
. Default:false
.
-
-
callback
<Function>-
err
<Error> -
stats
<fs.Stats>
-
异步统计(2)。回调有两个参数 (err, stats)
,其中 stats
是 <fs.Stats> 对象。
¥Asynchronous stat(2)
. The callback gets two arguments (err, stats)
where
stats
is an <fs.Stats> object.
如果出现错误,err.code
将是 常见系统错误 之一。
¥In case of an error, the err.code
will be one of Common System Errors.
fs.stat()
遵循符号链接。使用 fs.lstat()
查看链接本身。
¥fs.stat()
follows symbolic links. Use fs.lstat()
to look at the
links themselves.
不建议在调用 fs.open()
、fs.readFile()
或 fs.writeFile()
之前使用 fs.stat()
检查文件是否存在。而是,用户代码应该直接打开/读取/写入文件,并在文件不可用时处理引发的错误。
¥Using fs.stat()
to check for the existence of a file before calling
fs.open()
, fs.readFile()
, or fs.writeFile()
is not recommended.
Instead, user code should open/read/write the file directly and handle the
error raised if the file is not available.
要检查文件是否存在而不对其进行操作,建议使用 fs.access()
。
¥To check if a file exists without manipulating it afterwards, fs.access()
is recommended.
例如,给定以下目录结构:
¥For example, given the following directory structure:
- txtDir
-- file.txt
- app.js
下一个程序将检查给定路径的统计信息:
¥The next program will check for the stats of the given paths:
import { stat } from 'node:fs';
const pathsToCheck = ['./txtDir', './txtDir/file.txt'];
for (let i = 0; i < pathsToCheck.length; i++) {
stat(pathsToCheck[i], (err, stats) => {
console.log(stats.isDirectory());
console.log(stats);
});
}
结果输出将类似于:
¥The resulting output will resemble:
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
}