📜  Node.js fs.filehandle.sync() 方法

📅  最后修改于: 2022-05-13 01:56:31.574000             🧑  作者: Mango

Node.js fs.filehandle.sync() 方法

fs.filehandle.sync()方法是文件系统模块中fs.filehandle类的内置应用程序编程接口,用于将该文件的内核状态与存储设备同步。
句法:

const filehandle.sync()

参数:此方法不接受任何参数作为参数。
返回值:此方法返回一个未包含任何值的待处理承诺。
下面的程序说明了fs.filehandle.sync()方法的使用。
示例 1:文件名:index.js

javascript
// 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() {
 
    // Initializing 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);


javascript
// 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) => {
 
    // Catching 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() {
 
    // Initializing 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 :- Content of file: example.txt
data is synchronized successfully
content of file after operation :- Content of file: example.txt

示例 2:文件名:index.js

javascript

// 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) => {
 
    // Catching 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() {
 
    // Initializing 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

输出:

参考: https://nodejs.org/dist/latest-v12.x/docs/api/fs.html#fs_filehandle_sync