📜  Node.js fs.futimesSync() 方法

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

Node.js fs.futimesSync() 方法

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

句法:

fs.futimesSync( fd, atime, mtime )

参数:此方法接受三个参数,如上所述,如下所述:

  • fd:它是一个整数值,表示必须更改时间戳的文件的文件描述符。
  • atime:数字、字符串或日期对象,表示要设置的新访问时间戳。
  • mtime:数字、字符串或日期对象,表示要设置的新修改时间戳。

下面的示例说明了 Node.js 中的fs.futimesSync() 方法

示例 1:

// Node.js program to demonstrate the
// fs.futimesSync() 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 futimesSync() function to assign
// the new timestamps to the file descriptor
fs.futimesSync(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: 2015-12-20T19:42:00.000Z
Access Time: 2020-05-25T15:40:13.508Z

Details after changing time:
Changed Modification Time: 2020-05-25T15:52:21.546Z
Changed Access Time: 2020-05-25T15:52:21.546Z

示例 2:

// Node.js program to demonstrate the
// fs.futimesSync() 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("March 16, 2019 04:12:00");
let changedAccessTime = new Date("December 18, 2019 08:15:00");
  
// Use the futimesSync() function to assign
// the new timestamps to the file descriptor
fs.futimesSync(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:52:21.546Z
Access Time: 2020-05-25T15:52:21.604Z

Details after changing time:
Changed Modification Time: 2019-03-15T22:42:00.000Z
Changed Access Time: 2019-12-18T02:45:00.000Z

参考: https://nodejs.org/api/fs.html#fs_fs_futimessync_fd_atime_mtime