Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

list files or directories recursively in high efficiency

Thread view: 
Ken - 02 Dec 2005 07:36 GMT
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?
Roedy Green - 02 Dec 2005 18:49 GMT
>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.

Signature

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.