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:
add
event types and create the list to be returned reset
API to do this) The benefits of this approach would be:
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.