Results 1 to 9 of 9
  1. #1
    matzahboy is offline Member
    Join Date
    Nov 2008
    Posts
    67
    Rep Power
    0

    Default Writing a file listener

    I have a file that a program written in another language is going to change every few minutes. I want my java program to run a set of code (which I've already written) every time this file changes. How would I do this?

    The source path for the file is "C:\\outputFile.txt".

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I've never done a file listener, so sorry but I can't help you, but I can wish you a happy Matzah week.

  3. #3
    matzahboy is offline Member
    Join Date
    Nov 2008
    Posts
    67
    Rep Power
    0

    Default

    Do you think I should post this in the "advanced" section?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm not sure. If you do decide to do this though, post links from one post to the other.

  5. #5
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    You have to poll every so often to see if lastModified has changed. There is no event service from the file system in java.

  6. #6
    matzahboy is offline Member
    Join Date
    Nov 2008
    Posts
    67
    Rep Power
    0

    Default

    If I wanted to poll it every 20 seconds, how would I do that?

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default

    With a Timer.

    db

  8. #8
    matzahboy is offline Member
    Join Date
    Nov 2008
    Posts
    67
    Rep Power
    0

    Default

    I've done google searches on this. I've looked at websites/tutorials. I know that I need to use a timer and a filelistener. But how do I write the code? I understand what I need to use by not how to use it.

  9. #9
    einarb is offline Member
    Join Date
    Mar 2009
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by matzahboy View Post
    I've done google searches on this. I've looked at websites/tutorials. I know that I need to use a timer and a filelistener. But how do I write the code? I understand what I need to use by not how to use it.
    I've never heard of a filelistener interface.. My guess would be to use a Timer and a TimerTask, in some way like this

    Start the timer something like this
    Java Code:
    import java.util.Timer;
    
    public class SomeClass {
    	private static final long TIME_TO_START = ...;
    	private static final long DELAY_BETWEEN_POLLS = ...;
    	Timer timer = new Timer();
    	// ...
    
    	public void startFilePoller(File file) {
    		timer.scheduleAtFixedRate(new FileListener(file), TIME_TO_START, DELAY_BETWEEN_POLLS);
    	}
    	//...
    And you need an implementation of TimerTask, which will work somewhat similar to a listener
    Java Code:
    public class FileListener extends TimerTask {
    	// ...
    	public FileListener(File file, ..) {
    		this.file = file;
    		//..
    	}
    
    	@Override
    	public void run() {
    		if (isFileModified()) {
    			// do whatever needs doing if file has been modified
    		}
    	}
    
    	private boolean isFileModified(){
    		//...
    Hope this can get you started

Similar Threads

  1. Writing to DAT or TXT file
    By hunterbdb in forum Advanced Java
    Replies: 7
    Last Post: 10-12-2008, 02:50 PM
  2. Replies: 9
    Last Post: 07-10-2008, 04:52 PM
  3. Writing to a file (at the end)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:22 AM
  4. writing to a file
    By bugger in forum New To Java
    Replies: 1
    Last Post: 11-11-2007, 02:49 AM
  5. Help with File reading and writing
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 06:47 PM

Posting Permissions

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