Forums Register Login
How to create a thread?
How to create a thread that probes that directory and then process when a file appears?
Welcome to JavaRanch, Kok Hwa Koh!
I'm moving this thread to the Threads and Synchronization forum...
I think the only way to do this in pure Java would be to write a Thread with a run() method like
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.
You also may find java.util.Timer to be convenient for setting up a TimerTask which checks your directory periodically.
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards

This thread has been viewed 656 times.

All times above are in ranch (not your local) time.
The current ranch time is
Sep 20, 2018 12:18:47.