fs.promises.lstat()方法定義在 Node.js 的文件係統模塊中。文件係統模塊本質上是與用戶計算機的硬盤進行交互。 lstat() 方法使用 stats 對象上定義的方法(由 lstat 提供的數據)提供一些特定於文件和文件夾的信息。 fs.promises.lstat() 方法返回已解決或拒絕的 Promise,從而避免 fs.readdir() 方法中可能出現的回調嵌套或回調地獄問題。
用法
fs.promises.lstat(path, options)
參數:該方法接受如上所述和如下所述的兩個參數:
path:它是一個字符串、緩衝區或 url,指定我們嘗試讀取其內容的目錄路徑。
options:它是一個可選參數。其中一個選項參數是‘bigint’,它是一個布爾值。在這裏,我們通過 fs.lstat() 方法指定返回的 stats 對象中的數值是否為 bigint (default-false)。
返回值:它返回已解決或已拒絕的承諾。如果成功讀取目錄,則使用 stats 對象解決承諾,否則如果發生任何錯誤(example-specified 目錄不存在或沒有讀取文件的權限等),則使用錯誤對象拒絕。
從已解決的 Promise 返回的 stats 對象中定義了一些屬性和方法,這有助於獲取有關目標文件或文件夾的一些特定詳細信息。下麵指定了一些方法。
- 統計數據.isDirectory():如果 stats 對象說明文件係統目錄,則返回 true。
- 統計數據.isFile():如果 stats 對象說明常規文件,則返回 true。
- 統計數據.isSocket():如果 stats 對象說明了一個套接字,則返回 true。
- 統計數據.isSymbolicLink():如果 stats 對象說明了符號鏈接,則返回 true。
- 統計數據.isFile():如果 stats 對象說明常規文件,則返回 true。
- 統計數據.isFIFO():如果 stats 對象說明先進先出管道,則返回 true。
- 統計數據大小:指定文件的大小(以字節為單位)。
- 統計數據塊:指定為文件分配的塊數。
示例 1:
javascript
// Node.js program to demonstrate the
// Buffer.from() Method
// Importing File System module
const fs = require('fs')
// fs.readdir() reads contents of
// target directory
// process.cwd() gives current
// working directory
fs.readdir(process.cwd(), (err, filenames) => {
if (err) {
console.log(err)
return
}
for (let filename of filenames) {
// Calling lstat method to give the
// stats object for every directory
fs.promises.lstat(filename)
// If promise resolved and data
// are fetched
.then(stats => {
if (stats.isFile()) {
console.log(`${filename} ---------> File`)
} else {
console.log(`${filename} ---------> Folder`)
}
})
// If promise is rejected
.catch(err => {
console.log(err)
})
}
})
輸出:
示例 2:
javascript
// Node.js program to demonstrate the
// Buffer.from() Method
// Importing File System module
const fs = require('fs')
// The fs.readdir() method reads the
// contents of target directory
// The process.cwd() method gives the
// current working directory
fs.readdir(process.cwd(), async (err, filenames) => {
if (err) {
console.log(err)
return
}
for (let filename of filenames) {
// Calling lstat method to give the
// stats object for every directory
fs.promises.lstat(filename)
// If promise resolved and datas
// are fetched
.then(stats => {
console.log(
`${filename} --------> ${stats.size} bytes`)
})
// If promise is rejected
.catch(err => {
console.log(err)
})
}
})
輸出:
參考: https://nodejs.org/api/fs.html#fs_fspromises_lstat_path_options
相關用法
- Node.js fsPromises.lchmod()用法及代碼示例
- Node.js fsPromises.lchown()用法及代碼示例
- Node.js fsPromises.opendir()用法及代碼示例
- Node.js fsPromises.truncate()用法及代碼示例
- Node.js fsPromises.symlink()用法及代碼示例
- Node.js fsPromises.utimes()用法及代碼示例
- Node.js fsPromises.chmod()用法及代碼示例
- Node.js fsPromises.copyFile()用法及代碼示例
- Node.js fsPromises.rename()用法及代碼示例
- Node.js fsPromises.writeFile()用法及代碼示例
- Node.js fsPromises.readFile()用法及代碼示例
- Node.js fsPromises.access()用法及代碼示例
- Node.js fsPromises.mkdtemp()用法及代碼示例
- Node.js fsPromises.open()用法及代碼示例
- Node.js fsPromises.appendFile()用法及代碼示例
- Node.js fsPromises.mkdir()用法及代碼示例
- Node.js fsPromises.stat()用法及代碼示例
- Node.js fsPromises.realpath()用法及代碼示例
- Node.js fsPromises.chown()用法及代碼示例
- Node.js fsPromises.access(path[, mode])用法及代碼示例
- Node.js fsPromises.copyFile(src, dest[, mode])用法及代碼示例
- Node.js fsPromises.mkdtemp(prefix[, options])用法及代碼示例
- Node.js fsPromises.opendir(path[, options])用法及代碼示例
- Node.js fsPromises.readdir(path[, options])用法及代碼示例
注:本文由純淨天空篩選整理自hunter__js大神的英文原創作品 Node.js fsPromises.lstat() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。