Java 无限循环以检测最新文件

Java 无限循环以检测最新文件,java,file,Java,File,我需要运行一个无限循环,它显示一个新创建的文件的名称,其中在存储库中检测到一个新文件,然后停止它,直到创建一个新文件 String directoryPath = "/home/Maria/Bureau/file/"; File directory = new File(directoryPath); File moreRecentFile = null; File[] subfiles = directory.listFi

我需要运行一个无限循环,它显示一个新创建的文件的名称,其中在存储库中检测到一个新文件,然后停止它,直到创建一个新文件

        String directoryPath = "/home/Maria/Bureau/file/"; 
        File directory = new File(directoryPath); 

        File moreRecentFile = null; 
        File[] subfiles = directory.listFiles(); 
        if( subfiles.length > 0 )
        {
            moreRecentFile = subfiles[0]; 

        for (int i = 1; i < subfiles.length; i++) 

        { 
            File subfile = subfiles[i]; 

        if (subfile.lastModified() > moreRecentFile.lastModified())
            moreRecentFile = subfile;
        }

        System.out.println(moreRecentFile.getName()); 

        }
String directoryPath=“/home/Maria/Bureau/file/”;
文件目录=新文件(目录路径);
文件moreRecentFile=null;
File[]subfiles=directory.listFiles();
如果(子文件长度>0)
{
moreRecentFile=子文件[0];
对于(int i=1;imoreRecentFile.lastModified())
moreRecentFile=子文件;
}
System.out.println(moreRecentFile.getName());
}

我写了这个代码来检测新创建的文件,我想知道如何添加这个循环

现代操作系统和文件系统AFAIK支持所谓的文件系统事件,即创建侦听器并接收有关文件系统更改的事件,包括新文件创建事件

来自Oracle文档的F.e.:

(;;)的
{
//等待钥匙发出信号
监视键;
试一试{
key=watcher.take();
}捕捉(中断异常x){
返回;
}
for(WatchEvent事件:key.pollEvents()){
WatchEvent.Kind-Kind=event.Kind();
//此密钥仅注册
//对于输入和创建事件,
//但是溢出事件可以
//不管事件是否发生
//丢失或丢弃。
如果(种类==溢出){
继续;
}
//文件名是
//事件的背景。
WatchEvent ev=(WatchEvent)事件;
路径文件名=ev.context();
//验证新的
//文件是一个文本文件。
试一试{
//根据目录解析文件名。
//如果文件名为“test”,目录为“foo”,
//解析的名称是“test/foo”。
Path child=dir.resolve(文件名);
如果(!Files.probeContentType(child).equals(“text/plain”)){
System.err.format(“新文件'%s'”+
“不是纯文本文件。%n”,文件名);
继续;
}
}捕获(IOX异常){
系统错误println(x);
继续;
}
//将文件通过电子邮件发送到
//指定的电子邮件别名。
System.out.format(“通过电子邮件发送文件%s%n”,文件名);
//细节留给读者。。。。
}
//重置键——如果您想重置,此步骤至关重要
//接收进一步的监视事件。如果密钥不再有效,
//目录不可访问,请退出循环。
布尔有效值=key.reset();
如果(!有效){
打破
}
}

另一个很好的示例链接:

为什么不使用来让文件系统为您检测更改?我不知道WatchService@MariaBaccar我用文档和链接添加了答案example@MariaBaccar你已经在那里找到了链接。JDK1.6是否提供Watcheservice?@Mariabacar恐怕没有,根据javadoc,这些类从Java 1.7开始可用:
for (;;) {

    // wait for key to be signaled
    WatchKey key;
    try {
        key = watcher.take();
    } catch (InterruptedException x) {
        return;
    }

    for (WatchEvent<?> event: key.pollEvents()) {
        WatchEvent.Kind<?> kind = event.kind();

        // This key is registered only
        // for ENTRY_CREATE events,
        // but an OVERFLOW event can
        // occur regardless if events
        // are lost or discarded.
        if (kind == OVERFLOW) {
            continue;
        }

        // The filename is the
        // context of the event.
        WatchEvent<Path> ev = (WatchEvent<Path>)event;
        Path filename = ev.context();

        // Verify that the new
        //  file is a text file.
        try {
            // Resolve the filename against the directory.
            // If the filename is "test" and the directory is "foo",
            // the resolved name is "test/foo".
            Path child = dir.resolve(filename);
            if (!Files.probeContentType(child).equals("text/plain")) {
                System.err.format("New file '%s'" +
                    " is not a plain text file.%n", filename);
                continue;
            }
        } catch (IOException x) {
            System.err.println(x);
            continue;
        }

        // Email the file to the
        //  specified email alias.
        System.out.format("Emailing file %s%n", filename);
        //Details left to reader....
    }

    // Reset the key -- this step is critical if you want to
    // receive further watch events.  If the key is no longer valid,
    // the directory is inaccessible so exit the loop.
    boolean valid = key.reset();
    if (!valid) {
        break;
    }
}