fs.Dir.closeSync()方法是文件係統模塊中fs.Dir類的內置應用程序編程接口,用於同步關閉目錄的基礎資源句柄。
用法:
const dir.closeSync()
參數:此方法不接受任何參數。
返回值:此方法返回一個未定義的對象
下麵的程序演示了fs.Dir.closeSync()方法的使用。
範例1:
文件名:index.js
// Node.js program to demonstrate the
// dir.closeSync() 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);
console.log("All the dirent beofre operation:- ");
for (var i = 0; i <= 6; i++) {
console.log(((dir.readSync())).name);
}
// Synchronously closeSyncing the directory's
// underlying resource handle
const promise = dir.closeSync();
// Display the result
console.log("dir is closed successfully");
}
// Catching error
stop('./').catch(console.error);
使用以下命令運行index.js文件:
node index.js
輸出:
All the dirent beofre operation:- abcd.cer books.txt cert.cer certfile.cer certificate1.cer example.txt features dir is closed successfully
範例2:
文件名:index.js
// Node.js program to demonstrate the
// dir.closeSync() method
const fs = require('fs');
const fsPromises = fs.promises;
// Name of the directory to be created
let directo = './temp';
// Checking if the directory is present or not
if (!fs.existsSync(directo)){
fs.mkdirSync(directo);
}
// Initiating asynchronous function
async function funct(path) {
// Initializng dir
let dir = null;
let pathval = null;
try {
// Creating and initiating directory's
// underlying resource handle
dir = await fs.promises.opendir(
new URL('file:///F:/java/temp'));
console.log("All the dirent before operation:- "
+ ((dir.readSync())));
// Getting the path of the directory
// by using path() method
pathval = dir.path;
} finally {
if (dir) {
// Close the file if it is opened.
console.log("Path:- " + pathval);
console.log("All the dirent beofre operation:- "
+ ((dir.readSync())));
console.log("dir is closed successfully");
await dir.closeSync();
}
}
}
funct('./').catch(console.error);
使用以下命令運行index.js文件:
node index.js
輸出:
All the dirent before operation:- [object Object] Path:- F: All the dirent before operation:- [object Object] dir is closed successfully
參考: https://nodejs.org/dist/latest-v12.x/docs/api/fs.html#fs_dir_closesync
相關用法
- Node.js console.timeLog()用法及代碼示例
- Node.js GM operator()用法及代碼示例
- Node.js GM whiteThreshold()用法及代碼示例
- Node.js GM whitePoint()用法及代碼示例
- Node.js GM write()用法及代碼示例
- Node.js GM drawLine()用法及代碼示例
- Node.js GM drawEllipse()用法及代碼示例
- Node.js GM drawPolygon()用法及代碼示例
- Node.js GM drawBezier()用法及代碼示例
- Node.js GM drawArc()用法及代碼示例
- Node.js GM drawPolyline()用法及代碼示例
- Node.js GM drawCircle()用法及代碼示例
- Node.js GM channel()用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Node.js fs.Dir.closeSync() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。