热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

node.jsfs.filehandle.datasync()方法

node.jsfs.filehandle.datasync()方法

node . js fs . file handle . datasync()方法

原文:https://www . geesforgeks . org/node-js-fs-file handle-datasync-method/

fs.filehandle.datasync() 方法是文件系统模块内 fs.filehandle 类的内置应用编程接口,用于同步文件的数据。

语法:

const filehandle.datasync()

参数:此方法不接受任何参数。
返回值:该方法返回一个不包含任何价值的待定承诺。

下面的程序说明了 fs.filehandle.datasync() 方法的使用。

示例 1: 文件名:index.js

java 描述语言

// Node program to demonstrate the
// filehandle.datasync() method
const fs = require('fs');
const fsPromises = fs.promises;
console.log("File content before operation: "
        + (fs.readFileSync('example.txt')));
// Initiating asyncrionise function
async function funct() {
    // Initializing filehandle
    let filehandle = null;
    try {
        // Creating and initiating  filehandle
        filehandle = await
            fsPromises.open('example.txt', 'r+');
        // Syncing the data by using
        // datasync() method
        await filehandle.datasync();
    } finally {
        if (filehandle) {
            // Close the file if it is opened.
            console.log("data synced successfully");
            console.log("Content after operation: "
                + (fs.readFileSync('example.txt')));
            await filehandle.close();
        }
    }
}
funct().catch(console.error);

程序目录结构:

使用以下命令运行 index.js 文件:

node index.js

输出:

File content before operation: Content of the example.txt file
data synced successfully
Content after operation: Content of the example.txt file

示例 2: 文件名:index.js

java 描述语言

// Node.js program to demonstrate the
// filehandle.datasync() 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+');
        // Syncing the data by using
        // datasync() method
        await filehandle.datasync();
    } finally {
        if (filehandle) {
            // Close the file if it is opened.
            console.log("data synced 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 synced successfully
content of file after operation: This is a file containing a collection of books.

运行程序前的目录结构:

运行程序后的目录结构:

参考:https://nodejs . org/dist/latest-v 12 . x/docs/API/fs . html # fs _ file handle _ datasync


推荐阅读
author-avatar
shaka14
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有