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

node.jsfs.futtimes()方法

node.jsfs.futtimes()方法原文:h

node . js fs . fut times()方法

原文:https://www.geeksforgeeks.org/node-js-fs-futimes-method/

fs.futimes()方法用于异步更改给定文件描述符的修改和访问时间戳。可以使用数字、字符串或日期对象来指定时间戳。如果时间戳无法转换为正确的数字,或者是 NaN、Infinity 或-Infinity,则会引发错误。

语法:

fs.futimes( fd, atime, mtime, callback )

参数:该方法接受上述四个参数,描述如下:


  • FD: It is an integer that represents the file descriptor of the file whose timestamp must be changed.

  • atime: It means that the new access timestamp to be set is a number, string or Date object.

  • Time: It means that the new modification timestamp to be set is a number, string or Date object.

  • Callback: is a function that will be called when the method is executed.

    • err: If the method fails, this error will be thrown.



下面的例子说明了 Node.js 中的 fs.futimes()方法:

例 1:

// Node.js program to demonstrate the
// fs.futimes() method
// Import the filesystem module
const fs = require('fs');
// Get the file descriptor of the file
const fd = fs.openSync("example_file.txt", "r+");
console.log("Details before changing time:");
// Get the stats object of the file
prevStats = fs.statSync("example_file.txt");
// Access the modified and access time of the file
console.log("Modification Time:", prevStats.mtime);
console.log("Access Time:", prevStats.atime);
// Get the current time to change the timestamps
let changedModifiedTime = new Date();
let changedAccessTime = new Date();
// Use the futimes() function to assign
// the new timestamps to the file descriptor
fs.futimes(fd, changedAccessTime, changedModifiedTime, () => {
  // Get the stats object of the file
  console.log("\nDetails after changing time:");
  // Get the stats object of the file
  changedStats = fs.statSync("example_file.txt");
  // Access the changed modified and access time of the file
  console.log("Changed Modification Time:", changedStats.mtime);
  console.log("Changed Access Time:", changedStats.atime);
});

输出:

Details before changing time:
Modification Time: 2020-05-25T15:59:10.807Z
Access Time: 2020-05-25T15:59:10.807Z
Details after changing time:
Changed Modification Time: 2020-05-25T16:01:34.915Z
Changed Access Time: 2020-05-25T16:01:34.915Z

例 2:

// Node.js program to demonstrate the
// fs.futimes() method
// Import the filesystem module
const fs = require('fs');
// Get the file descriptor of the file
const fd = fs.openSync("example_file.txt", "r+");
console.log("Details before changing time:");
// Get the stats object of the file
prevStats = fs.statSync("example_file.txt");
// Access the modified and access time of the file
console.log("Modification Time:", prevStats.mtime);
console.log("Access Time:", prevStats.atime);
// Get the current time to change the timestamps
let changedModifiedTime = new Date("April 01, 2019 04:13:20");
let changedAccessTime = new Date("April 12, 2019 08:12:40");
// Use the futimes() function to assign
// the new timestamps to the file descriptor
fs.futimes(fd, changedAccessTime, changedModifiedTime, () => {
  // Get the stats object of the file
  console.log("\nDetails after changing time:");
  // Get the stats object of the file
  changedStats = fs.statSync("example_file.txt");
  // Access the changed modified and access time of the file
  console.log("Changed Modification Time:", changedStats.mtime);
  console.log("Changed Access Time:", changedStats.atime);
});

输出:

Details before changing time:
Modification Time: 2020-05-25T16:06:28.977Z
Access Time: 2020-05-25T16:06:28.977Z
Details after changing time:
Changed Modification Time: 2019-03-31T22:43:20.000Z
Changed Access Time: 2019-04-12T02:42:40.000Z

参考:https://nodejs . org/API/fs . html # fs _ fs _ futimes _ FD _ atime _ mtime _ callback


推荐阅读
  • node.jsrequire和ES6导入导出的区别原 ... [详细]
  • 本文介绍了Python对Excel文件的读取方法,包括模块的安装和使用。通过安装xlrd、xlwt、xlutils、pyExcelerator等模块,可以实现对Excel文件的读取和处理。具体的读取方法包括打开excel文件、抓取所有sheet的名称、定位到指定的表单等。本文提供了两种定位表单的方式,并给出了相应的代码示例。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • YOLOv7基于自己的数据集从零构建模型完整训练、推理计算超详细教程
    本文介绍了关于人工智能、神经网络和深度学习的知识点,并提供了YOLOv7基于自己的数据集从零构建模型完整训练、推理计算的详细教程。文章还提到了郑州最低生活保障的话题。对于从事目标检测任务的人来说,YOLO是一个熟悉的模型。文章还提到了yolov4和yolov6的相关内容,以及选择模型的优化思路。 ... [详细]
  • Linux服务器密码过期策略、登录次数限制、私钥登录等配置方法
    本文介绍了在Linux服务器上进行密码过期策略、登录次数限制、私钥登录等配置的方法。通过修改配置文件中的参数,可以设置密码的有效期、最小间隔时间、最小长度,并在密码过期前进行提示。同时还介绍了如何进行公钥登录和修改默认账户用户名的操作。详细步骤和注意事项可参考本文内容。 ... [详细]
  • 本文介绍了在Python3中如何使用选择文件对话框的格式打开和保存图片的方法。通过使用tkinter库中的filedialog模块的asksaveasfilename和askopenfilename函数,可以方便地选择要打开或保存的图片文件,并进行相关操作。具体的代码示例和操作步骤也被提供。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了使用kotlin实现动画效果的方法,包括上下移动、放大缩小、旋转等功能。通过代码示例演示了如何使用ObjectAnimator和AnimatorSet来实现动画效果,并提供了实现抖动效果的代码。同时还介绍了如何使用translationY和translationX来实现上下和左右移动的效果。最后还提供了一个anim_small.xml文件的代码示例,可以用来实现放大缩小的效果。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • 本文介绍了iOS数据库Sqlite的SQL语句分类和常见约束关键字。SQL语句分为DDL、DML和DQL三种类型,其中DDL语句用于定义、删除和修改数据表,关键字包括create、drop和alter。常见约束关键字包括if not exists、if exists、primary key、autoincrement、not null和default。此外,还介绍了常见的数据库数据类型,包括integer、text和real。 ... [详细]
  • Matlab 中的一些小技巧(2)
    1.Ctrl+D打开子程序  在MATLAB的Editor中,将输入光标放到一个子程序名称中间,然后按Ctrl+D可以打开该子函数的m文件。当然这个子程序要在路径列表中(或在当前工作路径中)。实际上 ... [详细]
author-avatar
帅帅考拉_955
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有