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
相關用法
- Node.js console.timeLog()用法及代碼示例
- Node.js GM thumbnail()用法及代碼示例
- Node.js GM threshold()用法及代碼示例
- Node.js GM segment()用法及代碼示例
- Node.js GM resize()用法及代碼示例
- Node.js GM raise()用法及代碼示例
- Node.js GM quality()用法及代碼示例
- Node.js GM transparent()用法及代碼示例
- Node.js GM roll()用法及代碼示例
- Node.js GM solarize()用法及代碼示例
- Node.js GM spread()用法及代碼示例
- Node.js GM motionBlur()用法及代碼示例
- Node.js GM operator()用法及代碼示例
- Node.js GM shave()用法及代碼示例
- Node.js GM scale()用法及代碼示例
注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 Node.js | fs.futimes() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。