Results 1 to 5 of 5
Thread: Detect new file entry
- 01-03-2010, 02:15 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 1
- Rep Power
- 0
- 01-03-2010, 06:25 PM #2
Member
- Join Date
- Jan 2010
- Posts
- 9
- Rep Power
- 0
You can determine if the file has been modified by using the lastModified() attribute
and storing an old value and if the value changes then update the old value and do whatever you want to do.
- 01-03-2010, 10:30 PM #3
Member
- Join Date
- Jan 2010
- Posts
- 13
- Rep Power
- 0
Hi rdemon,
If you only need to check if new files have been added, the easiest way is to use a collection class, such as java.util.Set or java.util.List (Set will probably be more efficient for a large number of files). You could then use something like the following:
public class CheckNewFiles
{
private Set<File> _lastFiles; // the set of files, last time we checked.
public CheckNewFiles()
{
_lastFiles = new HashSet<File>();
}
/// ...
public Set<File> getNewFiles(Set<File> currentFiles)
{
// clone the given list since we're about to modify it.
Set<File> newFiles = new HashSet<File>(currentFiles);
// remove everything that was in the list we previously detected, which will leave only the new files.
newFiles.removeAll(_lastFiles);
// this is for the next call
_lastFiles = currentFiles;
return newFiles;
}
}
It's not a complete class - it's just to give you an idea. You can call getNewFiles() periodically with the set of files that you currently monitored, and you will either get an empty set if there are no new files, or the set of files that were added.
Hope this helps :)
yanivby
- 01-07-2010, 09:45 PM #4
Member
- Join Date
- Jun 2008
- Posts
- 56
- Rep Power
- 0
I think that it should be somewhere open source solution. You just need to find it.
Try Controls4J - Advanced Swing Components.
- 01-08-2010, 02:24 AM #5
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 364
- Rep Power
- 4
I've been on this topic for some time.
Please copy, test and run this code:
Now, if you need to take special care about each file it could beJava Code:import java.io.File; import java.util.HashSet; import java.util.Set; /** * Listens to changes in destination folder * * To test this class: * 1. please change : FOLDER_PATH to directory that you need * 2. when app is started, do create some new file in destination folder and notice the change in console * @author dren * */ public class FileListener extends Thread { private Set<File> fileSet = new HashSet<File>(); /*set it on false from outside when you want to stop*/ private boolean runSignal = true; /*path to folder where to listen to file existence*/ private final String FOLDER_PATH = "C:\\temp"; /*sleep time*/ private final long SLEEP_TIME = 5*1000; //5 sec. public void run() { while(runSignal){ System.out.println("Let's check is there a new file..."); /*destination folder*/ File folder = new File(FOLDER_PATH); /*check file names in dir*/ if (folder.isDirectory()){ File[] fileList = folder.listFiles(); for(File one : fileList){ System.out.println(one.getName()); boolean isNewFile = fileSet.add(one); if (isNewFile) System.out.println("* * * New file is here: " + one.getName()); } } System.out.println("file set size:"+fileSet.size()); /*go to sleep*/ doSleep(); }//while } /** * sleep between listening folder iteration */ private void doSleep(){ try { System.out.println("I go to sleep for 5 sec now..."); sleep(SLEEP_TIME); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * stop running this thread when application stops */ public void stopRunning() { System.out.println("Bye!"); this.runSignal = false; } public static void main(String[] args) { FileListener fileListener = new FileListener(); fileListener.start(); //fileListener.stopRunning(); } }
useful to put all of them in some kind of a working queue.
This queue you can define as some Collection(Set, List...) of Files
and put it in separate class:
Java Code:import java.io.File; import java.util.LinkedList; /** * This class contains list of all Files that are detected * in folder that is listen to * * Files are added and remove from queue by different threads * @author dren * */ public class FileQueue { LinkedList<File> fileList = new LinkedList<File>(); /** * add File to File queue * @param file */ public synchronized void addFile(File file){ fileList.addLast(file); notify(); } /** * get file from queue or wait if list is empty * @return * @throws InterruptedException */ public synchronized File getFile() throws InterruptedException{ if (fileList.isEmpty()){ wait(); } return fileList.removeLast(); } }
File Thread Workers are used to remove file that has been processed
by your app logic in some way from working queue:
Java Code:import java.io.File; /** * This threads takes a File from a File Queue to do something with it * If queue is empty it waits until it becomes filled with new File * @author dren * */ public class FileWorker extends Thread{ private FileQueue fileQueue; FileWorker(FileQueue fq){ fileQueue = fq; } public void run() { while(true){ try { File f = fileQueue.getFile(); System.out.println(f.getName()); /*add some code to stop this execution*/ } catch (InterruptedException e) { e.printStackTrace(); } }//while } }
Now you have all concepts of what can be done.
Please show all of us your final version .
good luck :)
Similar Threads
-
Unable to detect OS name
By neetu.jainvi@gmail.com in forum Advanced JavaReplies: 1Last Post: 12-09-2009, 04:56 PM -
Detect the ENTER key being pressed
By aaronfsimons in forum New To JavaReplies: 12Last Post: 05-16-2009, 08:48 PM -
How to detect double click with a JFrame
By Jary316 in forum New To JavaReplies: 3Last Post: 01-05-2009, 08:39 PM -
Detect loading of ImageIcon from URL?
By barkster in forum Java AppletsReplies: 1Last Post: 01-29-2008, 07:04 PM -
How to detect USB device drives & set/remove file attributes
By sharafat in forum Advanced JavaReplies: 0Last Post: 01-20-2008, 06:07 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks