Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > list files or directories recursively in high efficiency

Reply
Thread Tools

list files or directories recursively in high efficiency

 
 
Ken
Guest
Posts: n/a
 
      12-02-2005
could someone here show me a demo how to write these kind of code that
list files or directories recursively in high efficiency just like
native program's speed?
Does the new feature NIO in JDK 1.4 could help?

 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      12-02-2005
On 1 Dec 2005 23:36:33 -0800, "Ken" <(E-Mail Removed)> wrote, quoted
or indirectly quoted someone who said :

>could someone here show me a demo how to write these kind of code that
>list files or directories recursively in high efficiency just like
>native program's speed?


I too have found File.list is pretty slow, even when Jet compiled.
see http://mindprod.com/jgloss/jet.html

Here is a method that uses two filters, one for directories and one
for files to find junk to get rid of:

/**
* Delete junk files in given fully qualified directory, and all
subdirs.
* uses current definition of junk. Wipes out files matching
definition of
* junk, not dirs.
*
* @param pDir
* fully qualified directory. Would normally be some
existing
* directory.
* @param pRecursive
* true if should also clean subdirs of this dir.
*/
public static void cleanDir ( File pDir, boolean pRecursive )
{
if ( pDir == null ) return;

// clean out child dirs
if ( pRecursive )
{
// all immediate child subdirs of this dir.
String[] allDirs = pDir.list( mDirFilter );
if ( allDirs != null )
{
for ( int i = 0; i < allDirs.length; i++ )
{
cleanDir( new File( pDir, allDirs[ i ] ), true );
}
}
}
// just junk files in this dir, not dirs themselves.
String[] allFiles = pDir.list( mJunkFilter );
if ( allFiles != null )
{
for ( int i = 0; i < allFiles.length; i++ )
{
deleteFile( new File( pDir, allFiles[ i ] ) );
}
}
}
I don't know WHY File.list is so slow. Before embarking on replacing
it with JNI check out its code to see if it doing anything obviously
stupid or overly generic that you could improve on.

I use 4NT which every once in while builds directory tree and stores
it as a flat file. It can find directories in a fraction of a second.
That approach might be suitable for some problems.

In the Replicator, I build an structure to look up files where
segments used to create names are stored as an array of interned
String constants to represent each name. This conserves RAM.

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
recursively remove all the directories and files which begin with '.' albert kao Python 5 05-16-2010 06:33 PM
recursively removing files and directories bahoo Python 2 04-09-2007 07:17 PM
recursively removing files and directories rbt Python 5 01-16-2006 03:19 PM
Recursively listing files into directories Alain.Feler Ruby 3 05-15-2005 06:09 PM
Help on Shell API to delete files/directories recursively Trinity C++ 1 11-07-2003 03:33 PM



Advertisments