How can I iterate over files in directory without an array?
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Paul_Lang
Posted On:   Monday, August 23, 2004 04:27 PM

Is there a way to "iterate" over the files in a directory without having to use one of java.io.File's methods that return an array? Basically, I don't want to read all the files into an array since that might take more memory than a target system may have available when there are tons of files in particular directory. Something like this would be nice... String dirName = "/path/to/dir/with/tons/of/files"; FileIterator i = new FileIterator(dirName); while(i.hasNext()){ String fileName = (String) i.next(); // do something with file }    More>>

Is there a way to "iterate" over the files in a directory without having to use one of java.io.File's methods that return an array?


Basically, I don't want to read all the files into an
array since that might take more memory than a target system may have available when there are tons of files in particular directory.


Something like this would be nice...


String dirName = "/path/to/dir/with/tons/of/files";

FileIterator i = new FileIterator(dirName);

while(i.hasNext()){

String fileName = (String) i.next();

// do something with file

}

   <<Less

Re: How can I iterate over files in directory without an array?

Posted By:   Anonymous  
Posted On:   Friday, October 8, 2004 01:55 PM

I think I have the perfect solution for you. If you don't need all the files but only certain files then use file filters. Try LatteLib (www.lattelib.com). Use the FileFinder. It returns a List which you can iterate. For example if you only need the GIF files that are older than five days and bigger than 1KB you could write the following:




List files = FileFinder.list("C:/dir/*.gif",
new PropertiesFileFilter().olderThanDays(5).biggerThan(1024));


If you wanted the same as above but also all the GIF files in subfolders recursively then you can use the following code:




List files = FileFinder.list("C:/dir//*.gif",
new PropertiesFileFilter().olderThanDays(5).biggerThan(1024));

Re: How can I iterate over files in directory without an array?

Posted By:   Simon_Ablett  
Posted On:   Tuesday, August 24, 2004 05:19 AM

I don't believe you can guarantee that the full list of files is not brought into memory in one hit. At least, there's no standard class that will provide such a facility. You could use the java.io.FileFilter class to reduce the number of files returned. Or shell out to the relevant 'dir' or 'ls' command, redirect the output to a file and then read that back in again but such a solution would not be particularily portable. Directories containing huge numbers of files tend not to be very efficient from an operating system point of view and I would suggest that you may want to revisit your storage strategy for them.

Regards.

Re: How can I iterate over files in directory without an array?

Posted By:   Michael_Decker  
Posted On:   Tuesday, August 24, 2004 12:30 AM

I'm not sure, but if you use a iterator, you 've perhaps the same problem, I think, because it will be saved inside the iterator and so you've more overhead because of using iterator.
About | Sitemap | Contact