监控文件夹下大小是否有变化

问题场景

在写定时器时遇到个问题,当定时器扫描某一固定文件夹下的内容进行处理时,可能这个文件夹还在被追加内容,这样定时器处理的内容就不准确了,为了解决它,笔者准备了一个哨兵,在每次轮到定时器处理前让哨兵进行一次检测,通过检测进行处理,否则这次任务跳过。

CODE

/**
     * 判断指定目录下文件大小是否变化
     *
     * @param dir    监控的目录绝对路径
     * @param period 统计时间间隔,ms
     * @return -1 error;   0 no change;    1 change
     */
    public static int isChange(String dir, long period) throws InterruptedException {
        return isChange(new File(dir), period);
    }

    /**
     * 判断指定目录下文件大小是否变化
     *
     * @param file   监控的文件
     * @param period 统计时间间隔,ms
     * @return -1 error;   0 no change;    1 change
     */
    public static int isChange(File file, long period) throws InterruptedException {
        int res = -1;
        if (file.exists()) {
            long sizeA = getAllSize4File(file);
            if (sizeA > 0) {
                Thread.sleep(period);
                long sizeB = getAllSize4File(file);
                long result = sizeB - sizeA;
                if (result == 0) {
                    res = 0;
                } else {
                    res = 1;
                }
            }
        }
        return res;
    }

    /**
     * 获取文件夹下的所有文件的大小
     *
     * @param path 文件夹绝对路径
     * @return
     */
    public static long getAllSize4File(String path) {
        return getAllSize4File(new File(path));
    }

    public static long getAllSize4File(File file) {
        long res = 0;
        if (file.exists()) {
            File[] files = file.listFiles();
            if (files != null) {
                for (File file1 : files) {
                    if (file1.isDirectory()) {
                        res += getAllSize4File(file1.getAbsolutePath());
                    } else {
                        res += file1.length();
                    }
                }
            } else {
                if (file.isFile()) {
                    res = file.length();
                }
            }
        }
        return res;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用C#提供的FileSystemWatcher类来实现监控文件夹和里面文件的变化。以下是一个简单的示例代码: ```csharp using System; using System.IO; class Program { static void Main(string[] args) { // 创建一个新的FileSystemWatcher对象 FileSystemWatcher watcher = new FileSystemWatcher(); // 设置要监控文件夹路径 watcher.Path = @"C:\myfolder"; // 监控所有类型的文件 watcher.Filter = "*.*"; // 监控文件变化的类型 watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; // 添加事件处理程序 watcher.Changed += new FileSystemEventHandler(OnChanged); watcher.Created += new FileSystemEventHandler(OnChanged); watcher.Deleted += new FileSystemEventHandler(OnChanged); watcher.Renamed += new RenamedEventHandler(OnRenamed); // 启动监控 watcher.EnableRaisingEvents = true; Console.WriteLine("Press any key to stop monitoring..."); Console.ReadKey(); } // 处理文件变化事件 private static void OnChanged(object source, FileSystemEventArgs e) { Console.WriteLine($"File: {e.FullPath} {e.ChangeType}"); } // 处理文件重命名事件 private static void OnRenamed(object source, RenamedEventArgs e) { Console.WriteLine($"File: {e.OldFullPath} renamed to {e.FullPath}"); } } ``` 这个示例代码会监控C:\myfolder文件夹中的所有文件变化,并在控制台输出变化的信息。你可以根据需要修改要监控文件夹路径、要监控的文件类型和要监控变化类型,并根据需要添加其他事件处理程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值