简体   繁体   中英

Java: Get a list of new files from a directory

I need a java program that on demands returns a list of newly added files to a directory. My definition of new file is the one which was not there at last checkup/scanning of the directory.

I know there are libraries such as JNotify, and java directory watch service http://docs.oracle.com/javase/tutorial/essential/io/notification.html . However in my understanding these libraries implement listeners that watch a directory and notify as soon as some file event (add, remove, delete, etc) occurs. But what I need is a method which on demand returns a list of newly added files to a directory.

You can achieve this easily by maintaining last request timestamp with the caller. Then pass that timestamp to a java class which will scan the directory for files created after that timestamp.

Something like:

public List<File> getNewFiles(Date lastRequestedDate)
{
//Iterate the directory for files newer than the date passed in as parameter
}

There are different approaches.

Approach one: File creation date

You might want to list all files and check the creation timestamp of each file. There's already a deep discussion on this topic . Just check if the timestamp is larger than the timestamp you saved when you last checked the directory.

Approach two: Create and maintain a list of files

If you can afford the memory and the time, create a list of files in the directory. Iterate over the directory and add all the files that are not already in the list to another list. Do whatever you need to do with them and add them to your main list.

You should also serialize your main list and save it to the disk after every successful checking and working with files.

您可以维护文件名列表,并对照新文件进行检查

I would suggest that you go with Java directory watch service. It should be fairly quick to implement a watch service. Have it running in the background; it will keep queueing events - add , remove , delete - as they happen. Whenever your on-demand API needs the list of files that have been added, you just need to do the following:

  • poll all the events that have been queued so far
  • iterate over them, look for the add event types and create the list to be returned
  • clear the events that you've polled so that they don't appear in the next poll (there is a reset API to do this)

The benefits of this approach would be:

  1. Fewer issues : You would have to scan directory and keep track of file being added; Java would be doing that for you, so there won't be any bugs in that part.
  2. Less memory consumption : With the timestamp approach, you will get files that have been modified since the last scan, so you would have to keep a list of all files you have encountered to identify file that have been added. But with the directory watch service you would have have to maintain any such data structure.
  3. Better performance : During the on-demand method execution, you would not be performing directory listing or file name comparisons; you would just iterate over the already queued events and create the return value.

You can maintain a file pointer in separate files.. and every time when you need new/older file you can pass into below method.

package test;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;

import org.apache.commons.io.comparator.LastModifiedFileComparator;
import org.apache.commons.io.filefilter.AgeFileFilter;

public class AgeFileFilterTest {

    public static void main(String[] args) throws IOException {
    String directoryPath = "/home/vikas";
    String fileName = "fileName";
    File directory = new File(directoryPath);


    File[] files = directory.listFiles();
    System.out.println("\nBefore ");
    displayFiles(directory, new AgeFileFilter(new File(directoryPath+FileName) , true));
    System.out.println("\nAfter " );
    displayFiles(directory, new AgeFileFilter(new File(directoryPath+FileName),false));

}

public static void displayFiles(File directory, FileFilter fileFilter) {
    File[] files = directory.listFiles(fileFilter);
    Arrays.sort(files, LastModifiedFileComparator.LASTMODIFIED_COMPARATOR);
    for (File file : files) {
        Date lastMod = new Date(file.lastModified());
        System.out.println("File: " + file.getName() + ", Date: " + lastMod + "");
    }
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM