list files or directories recursively in high efficiency

Discussion in 'Java' started by Ken, Dec 2, 2005.

  1. Ken

    Ken Guest

    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?
     
    Ken, Dec 2, 2005
    #1
    1. Advertising

  2. Ken

    Roedy Green Guest

    On 1 Dec 2005 23:36:33 -0800, "Ken" <> 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.
     
    Roedy Green, Dec 2, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Trinity
    Replies:
    1
    Views:
    528
    Peter van Merkerk
    Nov 7, 2003
  2. rbt
    Replies:
    5
    Views:
    9,017
  3. bahoo
    Replies:
    2
    Views:
    331
    Eli Criffield
    Apr 9, 2007
  4. albert kao
    Replies:
    5
    Views:
    725
    Walter Wefft
    May 16, 2010
  5. Alain.Feler
    Replies:
    3
    Views:
    128
    Ghislain Mary
    May 15, 2005
Loading...

Share This Page