fs.filehandle.sync()方法是文件係統模塊中fs.filehandle類的內置應用程序編程接口,用於將該文件的in-core狀態與存儲設備同步。
用法:
const filehandle.sync()
參數:此方法不接受任何參數作為參數。
返回值:此方法返回未包含的承諾。
下麵的程序演示了fs.filehandle.sync()方法的使用。
範例1: 文件名:index.js
// Node.js program to demonstrate the
// filehandle.sync() method
const fs = require('fs');
const fsPromises = fs.promises;
console.log("content of file before operation:- "
+ (fs.readFileSync('example.txt')));
// Initiating asyncrionise function
async function funct() {
// Initializng following variables
let filehandle = null;
let prom = null;
try {
// Creating and initiating filehandle
filehandle = await
fsPromises.open('example.txt', 'r+');
// Synchronizing the file's in-core state
// by using sync() method
prom = filehandle.sync();
} finally {
if (filehandle) {
// Display the result
console.log("data is synchronized successfully");
console.log("content of file after operation:- "
+ (fs.readFileSync('example.txt')));
// Close the file if it is opened.
await filehandle.close();
}
}
}
funct().catch(console.error);
運行程序之前的目錄結構:
運行程序後的目錄結構:
使用以下命令運行index.js文件:
node index.js
輸出:
content of file before operation:- Content of file:example.txt data is synchronized successfully content of file after operation:- Content of file:example.txt
範例2: 文件名:index.js
// Node.js program to demonstrate the
// filehandle.sync() method
const fs = require('fs');
const fsPromises = fs.promises;
// Data for the new file
let data = "This is a file containing"
+ " a collection of books.";
// Name of the file to be created
let file = "books.txt";
// Creating the new file 'books.txt'
fs.writeFile(file, data, (err) => {
// Cathing error
if (err) {
console.log(err);
}
});
// Using fs.exists() method
fs.exists(file, (exists) => {
if (exists) {
console.log("content of file before"
+ " operation:- " +
(fs.readFileSync(file)));
}
});
// Initiating asyncrionise function
async function funct() {
// Initializng filehandle
let filehandle = null;
try {
// Creating and initiating filehandle
filehandle = await
fsPromises.open(file, 'r+');
// Synchronizing the file's in-core
// state by using sync() method
prom = filehandle.sync();
} finally {
if (filehandle) {
// Close the file if it is opened.
console.log("data is synchronized"
+ " successfully");
console.log("content of file after"
+ " operation:- " +
(fs.readFileSync(file)));
await filehandle.close();
}
}
}
funct().catch(console.error);
運行程序之前的目錄結構:
運行程序後的目錄結構:
使用以下命令運行index.js文件:
node index.js
輸出:
content of file before operation:- This is a file containing a collection of books.
data is synchronized successfully
content of file after operation:- This is a file containing a collection of books.
參考: https://nodejs.org/dist/latest-v12.x/docs/api/fs.html#fs_filehandle_sync
相關用法
- Node.js console.timeLog()用法及代碼示例
- Node.js GM gaussian()用法及代碼示例
- Node.js GM crop()用法及代碼示例
- Node.js GM despeckle()用法及代碼示例
- Node.js GM quality()用法及代碼示例
- Node.js GM resize()用法及代碼示例
- Node.js GM transparent()用法及代碼示例
- Node.js GM raise()用法及代碼示例
- Node.js GM gamma()用法及代碼示例
- Node.js GM charcoal()用法及代碼示例
- Node.js GM blur()用法及代碼示例
- Node.js GM sharpen()用法及代碼示例
- Node.js GM whitePoint()用法及代碼示例
- Node.js GM whiteThreshold()用法及代碼示例
- Node.js GM drawCircle()用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Node.js fs.filehandle.sync() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。