Node.js文件系统之文件监听

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012054869/article/details/81982801

4.监听文件

      fs.watchFile()

      fs.watchFileSync()

      fs.watch

      FSWatcher类

// 导入模块
const fs = require('fs');

// 监听文件的变化
//curr当前的修改的状态,prev上一次文件修改的状态
// interval:1000每隔一段时间检测文件变化
fs.watchFile('../fs/zhang.txt', {interval: 1000}, function (curr, prev) {
    console.log('当前文件的修改时间:' + curr.mtime + '当前文件的大小:' + curr.size);
    console.log("上一次的文件修改时间:" + prev.mtime + '上一次文件的大小:' + prev.size);

    // 停止监听
    fs.unwatchFile('../fs/zhang.txt');
});

// 使用watch监听文件变化
// eventType事件类型,filename变化的文件名
fs.watch('../fs/2.txt', function (eventType, filename) {
    console.log(eventType, filename);//输出rename 2.txt
});

猜你喜欢

转载自blog.csdn.net/u012054869/article/details/81982801