当前位置: 首页 > 知识库问答 >
问题:

比较当前日期时间(实时)与文件上次修改

常子濯
2023-03-14

我希望我的程序输出这样,每当它从客户端接收文本文件时,它将打印“一个新文件已经收到!”。文本文件将存储在C:/users/%userprofile%/desktop/ad中。

下面的代码是检查目录中是否收到了新的文本文件。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.Timer;


public class Test {

    final DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    static int count = 0;
    static File[] f = null;
    static Date date = new Date();  
    static Calendar now = null;

    public static void main(String[] args) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        while(true) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
            f = new File("C:/Users/roberts/Desktop/ad").listFiles();
            int l = f.length;
            System.out.println("There are " +  l +  " files in the directory");

            for (int i = 0; i < l; i++) {
                System.out.println("file " + i +  " date modified " + dateFormat.format(f[i].lastModified()));
            }
        }
    }
}

目录中最初=1且最后修改的文件总数的输出

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.Timer;


public class Test {

    final DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    int interval = 1000;
    static int count = 0;
    static File[] f = null;
    static Date date = new Date();  
    static Calendar now = null;

    public static void main(String[] args) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        while(true) {

                f = new File("C:/Users/roberts/Desktop/ad").listFiles();
                int l = f.length;
                System.out.println(l);
                new Timer(1000, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        now = Calendar.getInstance();
                        for (int i = 0; i < l; i++) {
                            if(dateFormat.format(now.getTime()).equals(dateFormat.format(f[i].lastModified()))) {
                                System.out.println("A new file has been received!");
                            }
                        }

                    }
                }).start();

            try {
                Thread.currentThread().join();
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    }
}

我试过了,但它没有打印“一个新的文件已经收到!”。请帮忙

共有1个答案

沙富
2023-03-14

监视文件系统更改最好使用监视服务完成。现在您正在轮询文件更改,但watch服务使用的是本机windows事件。

首先,您只需要创建WatchService类的一个实例

Path path = Paths.get("c:\mydir");
WatchService service = path.getFileSystem().newWatchService();

接下来,您必须决定要监视哪种类型的事件。我认为您只希望在文件夹中出现新文件时得到通知。

path.register(service, StandardWatchEventKinds.ENTRY_CREATE);
// repeat forever
for (;;)
{
  // this will block until there's something inside the buffer
  WatchKey key = service.take();

  // 1 watchkey can contain multiple events, you need to iterate these.
  for (WatchEvent<?> event : key.pollEvents())
  {
    //todo: handle events
  }
}
 类似资料:
  • 我有个约会‘2019-05-08t 22:15:00-0400’。我想将这个日期与当前的日期和时间进行比较。如果上述日期和时间小于当前日期和时间,那么我必须做些什么。 我的代码是:- 但它返回 false 。

  • 问题内容: 我正在使用moment.js格式化我的日期时间,这里有两个日期值,当一个日期大于另一个日期时,我想实现一种特定的功能。我阅读了他们的大多数文档,但没有找到实现此目的的功能。我知道会在那里。 这是我的代码: 编辑 在这里,我正在比较两个日期,即,期望第一个日期大于第二个日期,但是它总是转到else条件。不知道为什么 问题答案: 我相信你正在寻找的查询功能,,,和。 但是要确切地说出您要尝

  • 我正在使用moment.js格式化我的日期时间,这里我有两个日期值,当一个日期大于另一个时,我想实现一个特定的函数。我读了他们的大部分文档,但没有找到实现这一点的函数。我知道它会在那里。 这是我的代码: 编辑 在这里,我比较了两个日期,即,希望第一个日期大于第二个日期,但它总是转到else条件。我不知道为什么?

  • 问题内容: 如何在Java中比较没有时间的日期? 此代码比较时间和日期! 问题答案: 尝试比较将时间更改为00:00:00的日期(使用此功能):

  • 问题内容: 我正在设计一个调度程序,并使用石英库。我想检查cron表达式时间是否是将来的时间,否则将根本不会执行触发器。有什么方法可以将cron表达式时间与Java中的当前时间进行比较。 问题答案: 需要考虑的是,标准的有效cron表达式将始终引用将来的有效时间。需要说明的是,Quartz cron表达式可能包含一个可选的Year字段,该字段可以是过去的以及将来的。 要检查表达式的有效性,您可以构

  • 问题内容: 我有两个String变量- 和。两者都包含格式为 HH:MM的值 。我该如何检查: 如果当前的时间内 和 会在最近一小时发生吗? 更新。 我实现了以下内容以转换为格式。但是它使用折旧方法: 问题答案: 将两个字符串转换为 对象(也是时间对象)创建一个新对象。 这将包含当前时间。 使用Date.before()和Date.after()方法来确定您是否在时间间隔内。 编辑:您应该能够直接