General J2EE: listing the files under a directory using Java API
I have a millions of files under a directory. I don't want get all the files from that directory using the following code snippet File file = new File("C:
- Posted by: kumar dave
- Posted on: January 26 2007 13:50 EST
files"); String files = file.list() or File files = file.listFiles(); which would kill my server. Is there a way such that i can get first 25000 files from directory and process them and get the next 25000 and process them and so on... I tried but didn't find help on this one Any help is greatly appreciated
- Re: listing the files under a directory using Java API by Ankur Kapadia on October 03 2007 21:01 EDT
- Re: listing the files under a directory using Java API by edmondo sena on September 13 2011 11:32 EDT
One way I found was to look beyond java for it, using the OS shell commands -- detecting if its windows or unix and running dir or ls with its various options Run the command as a thread and get access to the process's stdout / stderr and process the stream. It is usually quicker than JAVA API's access to the filesystem. It's a bit more complicated than the simple java api, but the performance improvement is worth it.
Could you write a code example for this problwem?