📜  Node.js fs.watchFile() 方法

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

Node.js fs.watchFile() 方法

fs.watchFile() 方法用于持续监视给定文件的更改。它有一个回调监听器,每次访问文件时都会调用它。它有一个可选的 options 参数,可用于指定属性,例如必须轮询文件的间隔时间,以及只要文件被监视,进程是否会继续。

监听器有两个参数,即当前统计对象和前一个统计对象。这可用于比较对文件的更改。修改后的文件访问时间可以从 fs.Stat 对象的 mtime 属性中找到。

在观看一个文件时,如果它消失并重新出现,消失回调的previousStat将与出现回调的previousStat相同。当文件被重命名然后再次重命名回其原始名称时会发生这种情况。当文件被删除然后恢复时也会发生这种情况。

句法:

fs.watchFile(filename[, options], listener)

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

  1. 文件名:它是一个字符串、缓冲区或 URL,表示要监视的文件的文件名。
  2. options:它是一个对象,可用于修改方法的行为。它是一个可选参数。它具有以下参数:
    • bigint:它是一个布尔值,用于将 fs.Stat 对象的数值指定为 BigInt 值。默认值为假。
    • 持久性:它是一个布尔值,用于指定只要文件正在被监视,进程是否应该继续。默认值是true。
    • 间隔:它是一个整数,指定每次轮询到目标之间的时间间隔。它以毫秒为单位指定。默认值为 5007。
  3. 侦听器:访问或修改文件时调用的函数。
    • current:它是一个 fs.Stats 对象,表示文件在被访问或修改后的当前状态。
    • previous:它是一个 fs.Stats 对象,表示文件在被访问或修改之前的先前状态。

返回值:函数调用成功时返回一个fs.StatWatcher对象。

下面的示例说明了 Node.js 中的fs.watchFile() 方法
示例 1:此示例显示 watchFile() 方法及其参数的用法。

// Node.js program to demonstrate
// the fs.watchFile() method
  
// Import the filesystem module
const fs = require('fs');
  
fs.watchFile(
  
  // The name of the file to watch
  "example_file.txt",
  
  // The options parameter is used to 
  //modify the behaviour of the method
  {
    // Specify the use of big integers
    // in the Stats object 
    bigint: false,
  
    // Specify if the process should 
    // continue as long as file is
    // watched
    persistent: true,
  
    // Specify the interval between
    // each poll the file
    interval: 4000,
  },
  (curr, prev) => {
    console.log("\nThe file was edited");
  
    // Show the time when the file was modified
    console.log("Previous Modified Time", prev.mtime);
    console.log("Current Modified Time", curr.mtime);
  
    console.log(
      "The contents of the current file are:",
      fs.readFileSync("example_file.txt", "utf8")
    );
  }
);
  
// Make Changes to the file for the first time
fs.writeFileSync("example_file.txt",
   "File Contents are Edited");
  
// Make Changes to the file for the second time
setTimeout(
  () => fs.writeFileSync("example_file.txt",
          "File Contents are Edited Again"),
  5000
);

输出:

The file was edited
Previous Modified Time 2020-05-30T07:52:14.587Z
Current Modified Time 2020-05-30T08:01:40.948Z
The contents of the current file are: File Contents are Edited

The file was edited
Previous Modified Time 2020-05-30T08:01:40.948Z
Current Modified Time 2020-05-30T08:01:45.950Z
The contents of the current file are: File Contents are Edited Again

示例2:此示例显示了文件重命名然后重命名回其原始名称时的文件修改时间,导致文件消失和重新出现。

// Node.js program to demonstrate 
// the fs.watchFile() method
  
// Import the filesystem module
const fs = require('fs');
  
fs.watchFile("example_file.txt", (curr, prev) => {
  console.log("\nThe File was modified");
  console.log("Previous Modification Time", prev.mtime);
  console.log("Current Modification Time", curr.mtime);
});
  
// Renaming the file to a new name
setTimeout(
  () => fs.renameSync("example_file.txt",
           "new_file.txt"),
  1000
);
  
// Renaming the file back to its old name
setTimeout(
  () => fs.renameSync("new_file.txt", 
          "example_file.txt"),
  6000
);

输出:

Previous Modification Time 2020-05-30T08:01:45.950Z
Current Modification Time 1970-01-01T00:00:00.000Z

The File was modified
Previous Modification Time 2020-05-30T08:01:45.950Z
Current Modification Time 2020-05-30T08:01:45.950Z

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