Results 1 to 5 of 5
  1. #1
    rdemon is offline Member
    Join Date
    Jan 2010
    Posts
    1
    Rep Power
    0

    Default Detect new file entry

    Hey all,

    i need to call a function when a thread detects when a certain directory receives a new file. i have a File [] to store the file list, i suppose i have to detect whenever this array changes, right? But how?

    thanks

  2. #2
    masternerdguy is offline Member
    Join Date
    Jan 2010
    Posts
    9
    Rep Power
    0

    Default

    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.

  3. #3
    yanivby is offline Member
    Join Date
    Jan 2010
    Posts
    13
    Rep Power
    0

    Default

    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

  4. #4
    devunion is offline Member
    Join Date
    Jun 2008
    Posts
    56
    Rep Power
    0

    Default

    I think that it should be somewhere open source solution. You just need to find it.
    Try Controls4J - Advanced Swing Components.

  5. #5
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    364
    Rep Power
    5

    Default

    I've been on this topic for some time.

    Please copy, test and run this code:


    Java 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(); 
    		
    	}
    	
    }
    Now, if you need to take special care about each file it could be
    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

  1. Unable to detect OS name
    By neetu.jainvi@gmail.com in forum Advanced Java
    Replies: 1
    Last Post: 12-09-2009, 04:56 PM
  2. Detect the ENTER key being pressed
    By aaronfsimons in forum New To Java
    Replies: 12
    Last Post: 05-16-2009, 08:48 PM
  3. How to detect double click with a JFrame
    By Jary316 in forum New To Java
    Replies: 3
    Last Post: 01-05-2009, 08:39 PM
  4. Detect loading of ImageIcon from URL?
    By barkster in forum Java Applets
    Replies: 1
    Last Post: 01-29-2008, 07:04 PM
  5. Replies: 0
    Last Post: 01-20-2008, 06:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •