• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to create a thread?  RSS feed

 
Kok Hwa Koh
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to create a thread that probes that directory and then process when a file appears?
Thanks.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Kok Hwa Koh!
I'm moving this thread to the Threads and Synchronization forum...
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the only way to do this in pure Java would be to write a Thread with a run() method like
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Though that only works if you already know the name of the file you're looking for. If you're looking for any new files in a directory, you can use listFiles() (in the File class) to see all the files in a given directory, then check the lastModified() method of each one to see if it's been recently changed (since the last time you checked that directory).
On some systems (Unix I think) you can shorcut this process by testing the lastModified() time of the directory itself - if new files have been added to the directory, the lastModified() time of the directory will show the time at whic files were added or deleted. So if that time has not changed, you don't need to check each individual file. However this doesn't work the same on all systems. So only rely on this if you've tested carefully on your system and you're sure the probram won't need to be run on a different system later. Hmmm, truth is, that's probably a dangerous assumption, so maybe you should just forget this paragraph unless it turns out that you need to detect new files really quickly and performance of the listFiles() method is a problem. Which is pretty unlikely I'd guess, but you never know.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You also may find java.util.Timer to be convenient for setting up a TimerTask which checks your directory periodically.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!