File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes Polling for a file on ftp using Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Polling for a file on ftp using Java" Watch "Polling for a file on ftp using Java" New topic

Polling for a file on ftp using Java

Vijay G Gandhi

Joined: Jul 10, 2007
Posts: 3

I have a file on my ftp server. I have to check if any file exists in the ftp directory, for every interval of 5 seconds.Could any one let me know, what is the best way to do it(file polling for every 5 secs) through java ?
Joe Ess

Joined: Oct 29, 2001
Posts: 9191

Welcome to the JavaRanch.
It is possible to do FTP using core Java classes. You can get a directory listing by requesting the directory instead of a file name, but then it's up to you to parse the directory listing.
The Apache Commons Net package can return a list of files, which would probably be easier to work with.

[How To Ask Questions On JavaRanch]
Vijay G Gandhi

Joined: Jul 10, 2007
Posts: 3
No I don't have to parse any directory on FTP ...because I have only one directory and one file in it....I have to poll for the file for every 5 seconds....that is I have to look up if file exists or not in the directory for every 5 seconds...and read it if it exists.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

If your question is "How do I do X every 5 minutes" and it's only incidental that "X" happens to be looking for a file on an FTP directory, the answer to that is that you use a java.util.Timer object.

Your question was really in two parts and you didn't make it clear which of the two parts you were more interested in.
Vijay G Gandhi

Joined: Jul 10, 2007
Posts: 3
First of all ....Thanks for your answer Paul...sorry for the ambiguity in my question....I will try with Timer class but
1) if the file is not uploaded completely on FTP server that is if some thing is writing to the file how would I know if my polling is for every 5 seconds.

2) If I would schedule timer for every 5 seconds....what if I am in the middle of reading first file (say very long file takes 15 seconds to read) and if my timer is polling for the file again after 5 seconds ?

Please let me know if i am clear with my questions...wud appreciate any advice...Thank you...
I agree. Here's the link:
subject: Polling for a file on ftp using Java
It's not a secret anymore!