當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Node.js fs.filehandle.utimes()用法及代碼示例

fs.filehandle.utimes()方法是“文件係統”模塊中fs.filehandle類的內置應用程序編程接口,用於更改此文件係統的時間戳。

用法:

const filehandle.utimes(atime, mtime)

參數:該方法接受上述和以下所述的兩個參數:

  • atime:訪問時間戳是最後一次讀取文件的時間。
  • mtime:修改的時間戳表示上次修改文件內容的時間。

返回值:此方法返回未包含的承諾。

以下示例程序旨在說明Node.js中fs.filehandle.utimes()方法的使用:



範例1: 文件名:index.js

// Node.js program to demonstrate the  
// filehandle.utimes() method 
const fs = require('fs'); 
const fsPromises = fs.promises; 
  
console.log("content of the file before operation:- "
        + fs.readFileSync('example.txt', 'utf8')); 
  
// File cTime beofre operation 
fs.stat('example.txt', (err, stats) => { 
    if (err) throw err; 
  
    console.log("CTime of the file before operation:"
                    + stats.ctime); 
}); 
  
// Initiating asyncrionise function 
async function funct() { 
  
    // Initializng following variables 
    let filehandle = null; 
    let prom = null; 
  
    try { 
  
        // Creating and initiating  filehandle 
        filehandle = await  
            fsPromises.open('example.txt', 'r+'); 
  
        // Changing the timestamp of the file 
        // by using utimes() method 
        prom = filehandle.utimes(0, 10); 
  
    } finally { 
  
        if (filehandle) { 
  
            // File cTime after operation 
            (filehandle.stat(true)) 
                        .then(function (result) { 
                console.log("CTime of the file "
                        + "after operation:- " 
                        + result.ctime); 
            }) 
  
            console.log("content of the file "
                    + "after operation:" + 
                fs.readFileSync('example.txt', 'utf8')); 
  
            // Close the file if it is opened. 
            await filehandle.close(); 
        } 
    } 
} 
  
funct().catch(console.error);

運行程序之前的目錄結構:

運行程序後的目錄結構:

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

node index.js

輸出:

content of the file before operation:Content of example.txt file
CTime of the file before operation:Tue Jul 07 2020 09:21:11 GMT+0530 (India Standard Time)
content of the file after operation:Content of example.txt file
CTime of the file after operation:- Tue Jul 07 2020 09:53:15 GMT+0530 (India Standard Time)

範例2: 文件名:index.js

// Node.js program to demonstrate the  
// filehandle.utimes() 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) => { 
  
    // Cathing 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))); 
    } 
}); 
  
// File cTime beofre operation 
fs.stat(file, (err, stats) => { 
    if (err) throw err; 
  
    console.log( 
        "CTime of the file before operation:"
        + stats.ctime); 
}); 
  
// Initiating asyncrionise function 
async function funct() { 
  
    // Initializng filehandle 
    let filehandle = null; 
  
    try { 
  
        // Creating and initiating  filehandle 
        filehandle = await  
            fsPromises.open(file, 'r+'); 
  
        // Changing the timestamp of the file 
        // by using utimes() method 
        prom = filehandle.utimes(0, 20); 
  
    } finally { 
  
        if (filehandle) { 
  
            // Close the file if it is opened. 
            // file cTime after operation 
            (filehandle.stat(true)) 
                    .then(function (result) { 
                console.log("CTime of the file "
                        + "after operation:- " 
                        + result.ctime); 
            }) 
  
            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.
CTime of the file before operation:Tue Jul 07 2020 09:56:52 GMT+0530 (India Standard Time)
content of file after operation:This is a file containing a collection of books.
CTime of the file after operation:Tue Jul 07 2020 09:57:09 GMT+0530 (India Standard Time)

參考: https://nodejs.org/dist/latest-v12.x/docs/api/fs.html#fs_filehandle_utimes_atime_mtime




相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Node.js fs.filehandle.utimes() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。