Java File Path Delete deleteEmptyParents(final File path)

Here you can find the source of deleteEmptyParents(final File path)

Description

Delete path and any empty parent directories.

License

Open Source License

Parameter

Parameter Description
path directory to start in.

Declaration

public static void deleteEmptyParents(final File path) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;

public class Main {
    /**//from www .  j a va  2  s  .c o m
     * Delete path and any empty parent directories.
     * 
     * @param path
     *            directory to start in.
     */
    public static void deleteEmptyParents(final File path) {
        deleteEmptyParents(path, null);
    }

    /**
     * Delete path and any empty parent directories up to the stopAt point.
     * 
     * @param path
     *            direcotry to start in
     * @param stopAt
     *            directory to stop at
     */
    public static void deleteEmptyParents(final File path, final File stopAt) {
        File parent = path;
        // Loop while parent is not null and we are not yet at the stopAt point
        while (parent != null && !parent.equals(stopAt)) {
            try {
                parent.delete();
                parent = parent.getParentFile();
            } catch (Exception e) {
                e.printStackTrace();
                break;
            }
        }
    }
}

Related

  1. deleteDirs(File path)
  2. deleteDirs(String pathname)
  3. deleteDirWithContent(final String path)
  4. deleteEmptDir(String Path, String username)
  5. deleteEmptyDir(File dstPath)
  6. deleteFile(File path)
  7. deleteFile(File path)
  8. deleteFile(File path)
  9. deleteFile(final String dirPath, final String fileName)