此篇文章瀏覽量:
649
檔案系統模組(fs),都提供了「非同步」和「同步」兩個版本的操作方法,此文章介紹 取得檔案資訊 的操作方式,僅示範同步(statSync)的方式。
取得檔案資訊有四個方法,分別是 fs.stat()、fs.statSync()、fs.fstat()、fs.fstatSync(),這裡僅示範fs.statSync() 方法。
「同步」取得檔案資訊:fs.statSync()
// 假設已經存在 txt/statSync.txt 檔案,取得該檔案資訊: var fs = require('fs'); var statSync = fs.statSync('txt/statSync.txt'); console.info(statSync);
取得的資訊如下:
Stats {
dev: 16777220,
mode: 33188,
nlink: 1,
uid: 501,
gid: 20,
rdev: 0,
blksize: 4096,
ino: 111646722,
size: 9,
blocks: 8,
atime: 2017-09-20T03:38:35.000Z,
mtime: 2017-09-20T03:38:34.000Z,
ctime: 2017-09-20T03:38:34.000Z,
birthtime: 2017-09-20T03:38:23.000Z }