Changeset 11161


Ignore:
Timestamp:
May 23, 2018 8:20:56 AM (19 months ago)
Author:
jsten
Message:

Improved FileUtil#recursiveDelete() so that it handles non-existing files and regular files. Ticket #5432.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Compiler/ModelicaFrontEnd/src/java/org/jmodelica/util/files/FileUtil.java

    r10047 r11161  
    231231
    232232    /**
    233      * Deletes a directory, any of its sub-directories, and all files within the
    234      * directories.
    235      *
    236      * @param directory
    237      *            The directory to delete.
    238      */
    239     public static void recursiveDelete(File directory) {
    240         for (File file : directory.listFiles()) {
    241             if (file.isDirectory()) {
    242                 recursiveDelete(file);
    243             } else {
    244                 file.delete();
    245             }
    246         }
    247         directory.delete();
     233     * Deletes a file or directory recursively.
     234     * <p>
     235     * If a file is provided, that file is deleted, if a directory is provided,
     236     * then all child files and directories are deleted recursively first.
     237     * <p>
     238     * The return argument of this method indicates whether the delete was
     239     * successful, basically an aggregate of the return value for
     240     * {@link File#delete()}.
     241     *
     242     * @param file The file or directory to delete.
     243     * @return True if the file doesn't exist, or if, file or entire directory
     244     *              structure was deleted, otherwise false.
     245     */
     246    public static boolean recursiveDelete(File file) {
     247        if (!file.exists()) {
     248            return true;
     249        }
     250        boolean res = true;
     251        if (file.isDirectory()) {
     252            for (File subFile : file.listFiles()) {
     253                res &= recursiveDelete(subFile);
     254            }
     255        }
     256        res &= file.delete();
     257        return res;
    248258    }
    249259
Note: See TracChangeset for help on using the changeset viewer.