Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

about files in a folder

Status
Not open for further replies.

onlymullapudi

Programmer
Apr 20, 2006
9
US
I want to write a progra in java, it should function like, whenever a new file is created in a folder, it should get the details of the new file in the folder autmatically(like the name of the file).I think it can be done by keeping the program running recursevely, can anyone tell how to write te code.
 
You need to poll the directory in a loop with a preset wait duration in between. Probably best to run this in its own thread but that depends upon your requirements. In the loop between attempts you would sleep the thread for some duration.

Tim
 
pseudo code :

Code:
File dir = ..
File[] oldFiles ..
File[] newFiles ..

list dir to get the list of files into 'oldFiles' variable

while (true) {
  list files into 'newFiles'
  cheeck the length - if there are more files than in 'oldFiles'
   then loop both, to find the one that is new

   Thread.sleep(1000/*millis*/);
}

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
You may even want to check the timestamps on the files and pick up files which have appeared / changed since the last time you checked.

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top