fs.stat()方法用於返回有關給定文件或目錄的信息。它返回一個fs.Stat對象,該對象具有幾個屬性和方法來獲取有關文件或目錄的詳細信息。
用法:
fs.stat( path, options, callback )
參數:此方法接受上述和以下所述的三個參數:
- path:它包含必須檢查的文件或目錄的路徑。它可以是字符串,緩衝區或URL。
- options:該對象可用於指定將影響輸出的可選參數。它具有一個可選參數:
- bigint:它是一個布爾值,它指定fs.Stats對象中返回的數值是否為bigint。默認值為false。
- callback:執行該方法時將調用該函數。
- err:如果該方法會引發錯誤
- stats:Stats對象包含文件路徑的詳細信息。
以下示例說明了Node.js中的fs.stat()方法:
範例1:本示例使用fs.stat()方法獲取路徑的詳細信息。
// Node.js program to demonstrate the
// fs.stat() method
// Import the filesystem module
const fs = require('fs');
// Getting information for a file
fs.stat("example_file.txt", (error, stats) => {
if (error) {
console.log(error);
}
else {
console.log("Stats object for:example_file.txt");
console.log(stats);
// Using methods of the Stats object
console.log("Path is file:", stats.isFile());
console.log("Path is directory:", stats.isDirectory());
}
});
// Getting information for a directory
fs.stat("example_directory.txt", (error, stats) => {
if (error) {
console.log(error);
}
else {
console.log("Stats object for:example_directory.txt");
console.log(stats);
// Using methods of the Stats object
console.log("Path is file:", stats.isFile());
console.log("Path is directory:", stats.isDirectory());
}
});
輸出:
Stats object for:example_file.txt Stats { dev:2269, mode:33188, nlink:1, uid:1000, gid:1000, rdev:0, blksize:4096, ino:271, size:0, blocks:0, atimeMs:1582871562365.894, mtimeMs:1582871556897.5554, ctimeMs:1582871556897.5554, birthtimeMs:1582871556897.5554, atime:2020-02-28T06:32:42.366Z, mtime:2020-02-28T06:32:36.898Z, ctime:2020-02-28T06:32:36.898Z, birthtime:2020-02-28T06:32:36.898Z } Path is file:true Path is directory:false Stats object for:example_directory.txt Stats { dev:2269, mode:33188, nlink:1, uid:1000, gid:1000, rdev:0, blksize:4096, ino:270, size:0, blocks:0, atimeMs:1582871562357.8936, mtimeMs:1582871553413.3396, ctimeMs:1582871553417.3398, birthtimeMs:1582871553413.3396, atime:2020-02-28T06:32:42.358Z, mtime:2020-02-28T06:32:33.413Z, ctime:2020-02-28T06:32:33.417Z, birthtime:2020-02-28T06:32:33.413Z } Path is file:true Path is directory:false
範例2:本示例使用fs.stat()方法使用bigint選項獲取文件的詳細信息。
// Node.js program to demonstrate the
// fs.stat() method
// Import the filesystem module
const fs = require('fs');
fs.stat("example_file.txt", (error, stats) => {
console.log(stats);
});
// Using the bigint option to return
// the values in big integer format
fs.stat("example_file.txt", { bigint:true }, (error, stats) => {
console.log(stats);
});
輸出:
Stats { dev:2269, mode:33188, nlink:1, uid:1000, gid:1000, rdev:0, blksize:4096, ino:271, size:0, blocks:0, atimeMs:1582871562365.894, mtimeMs:1582871556897.5554, ctimeMs:1582871556897.5554, birthtimeMs:1582871556897.5554, atime:2020-02-28T06:32:42.366Z, mtime:2020-02-28T06:32:36.898Z, ctime:2020-02-28T06:32:36.898Z, birthtime:2020-02-28T06:32:36.898Z } Stats { dev:2269n, mode:33188n, nlink:1n, uid:1000n, gid:1000n, rdev:0n, blksize:4096n, ino:271n, size:0n, blocks:0n, atimeMs:1582871562365n, mtimeMs:1582871556897n, ctimeMs:1582871556897n, birthtimeMs:1582871556897n, atime:2020-02-28T06:32:42.365Z, mtime:2020-02-28T06:32:36.897Z, ctime:2020-02-28T06:32:36.897Z, birthtime:2020-02-28T06:32:36.897Z }
參考: https://nodejs.org/api/fs.html#fs_fs_stat_path_options_callback
相關用法
注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 Node.js | fs.stat() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。