fs.Dir.readSync()方法是文件係統模塊內的類fs.Dir的內置應用程序編程接口,用於讀取目錄的每個下一個目錄。
用法:
const dir.readSync()
參數:此方法不接受任何參數。
返回值:此方法返回目錄的目錄。
以下程序說明了Node.js中fs.Dir.readSync()方法的使用:
範例1:
文件名:index.js
// Node.js program to demonstrate the
// dir.readSync() method
const fs = require('fs');
// Initiating asyn function
async function stop(path) {
// Creating and initiating directory's
// underlying resource handle
const dir = await fs.promises.opendir(path);
// Synchronously reading the directory's
// underlying resource handle
const dirent = dir.readSync();
// Display the result
console.log(dirent.name);
}
// Catching error
stop('./').catch(console.error);
使用以下命令運行index.js文件:
node index.js
輸出:
abcd.cer
範例2:
文件名:index.js
// Node.js program to demonstrate the
// dir.readSync() method
const fs = require('fs');
// Initiating asyn function
async function stop(path) {
let dir = null;
try {
// Creating and initiating directory's
// underlying resource handle
dir = await fs.promises.opendir(
new URL('file:///F:/java/'));
// Synchronously reading the directory's
// underlying resource handle
// using readSync() method
for(var i = 0 ; i <= 3 ; i ++ ) {
var dirent = dir.readSync();
// Display the result
console.log(dirent.name);
}
} finally {
if (dir) {
// Display the result
console.log("dir is closed successfully");
// Synchronously closeSyncing the
// directory's underlying resource
// handle
const promise = dir.closeSync()
}
}
}
// Catching error
stop('./').catch(console.error);
使用以下命令運行index.js文件:
node index.js
輸出:
abcd.cer cert.cer certfile.cer certificate1.cer dir is closed successfully
參考: https://nodejs.org/dist/latest-v12.x/docs/api/fs.html#fs_dir_readsync
相關用法
- Node.js console.timeLog()用法及代碼示例
- Node.js GM whitePoint()用法及代碼示例
- Node.js GM operator()用法及代碼示例
- Node.js GM sharpen()用法及代碼示例
- Node.js GM spread()用法及代碼示例
- Node.js GM solarize()用法及代碼示例
- Node.js GM shave()用法及代碼示例
- Node.js GM emboss()用法及代碼示例
- Node.js GM despeckle()用法及代碼示例
- Node.js GM blur()用法及代碼示例
- Node.js GM charcoal()用法及代碼示例
- Node.js GM whiteThreshold()用法及代碼示例
- Node.js GM write()用法及代碼示例
- Node.js GM drawLine()用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Node.js fs.Dir.readSync() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。