Java File Path Delete deleteEmptyDir(File dstPath)

Here you can find the source of deleteEmptyDir(File dstPath)

Description

deletes Empty directories

License

Open Source License

Parameter

Parameter Description
dstPath a parameter

Declaration

private static boolean deleteEmptyDir(File dstPath) 

Method Source Code


//package com.java2s;
/*/*from  ww  w  .  j  a  v  a  2  s  . c  o  m*/
 * Copyright 2007 Pentaho Corporation. All rights reserved.
 * 
 * This software was developed by Pentaho Corporation and is provided under the terms
 * of the Mozilla Public License, Version 1.1, or any later version. You may not use
 * this file except in compliance with the license. If you need a copy of the license,
 * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
 * BI Platform. The Initial Developer is Pentaho Corporation.
 *
 * Software distributed under the Mozilla Public License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
 * the license for the specific language governing your rights and limitations.
 *
 * Created
 * @author
 * 
 */

import java.io.File;

import java.io.IOException;

public class Main {
    /**
     * deletes Empty directories
     * 
     * @param dstPath
     * @return
     */
    private static boolean deleteEmptyDir(File dstPath) {

        Boolean result = false;
        if (dstPath.exists()) {
            try {
                delete(dstPath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * deletes Empty directories
     * 
     * @param file
     * @throws IOException
     */
    private static void delete(File file) throws IOException {

        if (file.isDirectory()) {
            if (file.list().length == 0) {
                file.delete();
            } else {
                String files[] = file.list();
                for (String temp : files) {
                    File fileDelete = new File(file, temp);
                    delete(fileDelete);
                }
                if (file.list().length == 0) {
                    file.delete();
                }
            }
        }
    }
}

Related

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