Changeset 10546 for trunk/gli/src/org


Ignore:
Timestamp:
2005-08-21T13:49:36+12:00 (19 years ago)
Author:
mdewsnip
Message:

Tidied up the delete function so it is hopefully slightly more efficient: the exists() test is now only done on the top-level file or folder.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/util/Utility.java

    r10544 r10546  
    3535 *########################################################################
    3636 */
     37
    3738package org.greenstone.gatherer.util;
     39
    3840
    3941// Don't even think about adding import java.awt.* here!
     
    4446// Don't even think about adding import javax.swing.* here!
    4547// The functions in this class should not use any graphical classes. Put your function somewhere else buster!
    46 import org.apache.xerces.parsers.*;
    47 import org.apache.xerces.dom.DocumentImpl;
    48 import org.apache.xml.serialize.*;
    4948import org.greenstone.gatherer.Dictionary;
    5049// Don't even think about adding import org.greenstone.gatherer.Gatherer in here!
    5150// The functions in this class should be independent of the Gatherer class. Put your function somewhere else buster!
    52 import org.w3c.dom.*;
    53 import org.xml.sax.*;
    5451
    5552
     
    9188    static final public String PROGRAM_VERSION = "v2.61";
    9289    static final public String WORKSPACE_TREE = "Workspace";
    93    
     90
    9491
    9592    /**
    9693     * Delete a file or directory
    97      * It turns out that in Java you have to make sure a directory is empty before you delete it (much like unix I suppose),
    98      * and so just like unix I'll have to set up a recursive delete function.
    99      *
    10094     * @param file The <strong>File</strong> you want to delete.
    10195     * @return A <i>boolean</i> which is <i>true</i> if the file specified was successfully deleted, <i>false</i> otherwise.
     
    108102    }
    109103
     104    return deleteInternal(file);
     105    }
     106
     107
     108    /** Convenience function. */
     109    static public boolean delete(String filename)
     110    {
     111    return delete(new File(filename));
     112    }
     113
     114
     115    /** In Java you have to make sure a directory is empty before you delete it, so recursively delete. */
     116    static private boolean deleteInternal(File file)
     117    {
    110118    // If file is a directory, we have to recursively delete its contents first
    111119    if (file.isDirectory()) {
    112120        File files[] = file.listFiles();
    113121        for (int i = 0; i < files.length; i++) {
    114         if (delete(files[i]) == false) {
     122        if (deleteInternal(files[i]) == false) {
    115123            System.err.println("Error: Could not delete folder " + file);
    116124            return false;
     
    126134
    127135    return true;
    128     }
    129 
    130 
    131     static public boolean delete(String filename) {
    132     return delete(new File(filename));
    133136    }
    134137
Note: See TracChangeset for help on using the changeset viewer.