方法說明
根據檔案描述符取得檔案資訊,適合已經用 fs.open 開啟檔案、想在不重新解析路徑的情況下取得 metadata 的情境。
語法
fs.fstat(fd, [callback(err, stats)])
此方式屬於 fs 模組,使用前必須先引入:var fs = require("fs")
參數說明
fd:檔案描述符。
callback:回調函式,接收 err 異常參數與 stats 檔案資訊物件。
stats 包含以下欄位(以下為範例中讀取到的實際值,並非固定預設值):
{
dev : 0,
mode : 33206,
nlink : 1,
uid : 0,
gid : 0,
rdev : 0,
ino : 0,
size : 378, // 位元組
atime : Tue Jun 10 2014 13:57:13 GMT +0800 <中國標準時間>,
mtime : Tue Jun 13 2014 09:48:31 GMT +0800 <中國標準時間>,
ctime : Tue Jun 10 2014 13:57:13 GMT +0800 <中國標準時間>
}
範例
var fs = require('fs');
fs.open('content.txt', 'a', function(err, fd){
if (err) {
throw err;
}
console.log('file open');
fs.fstat(fd, function(err, stats){
if (err) {
throw err;
}
console.log(stats);
fs.close(fd, function(){
console.log('file close');
});
});
});
原始碼
fs.fstat = function(fd, callback) {
callback = makeCallback(callback);
binding.fstat(fd, callback);
};