当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Node.js fs.futimes()用法及代码示例


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

用法:

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

参数:此方法接受上述和以下所述的四个参数:

  • fd:它是一个整数,表示必须更改其时间戳的文件的文件描述符。
  • atime:它是数字,字符串或Date对象,表示要设置的新访问时间戳。
  • mtime:是数字,字符串或Date对象,表示要设置的新修改时间戳。
  • callback:该方法执行时将调用该函数。
    • err:如果方法失败,将引发错误。

以下示例说明了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




相关用法


注:本文由纯净天空筛选整理自sayantanm19大神的英文原创作品 Node.js | fs.futimes() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。