Java File Path Delete deleteParent(String filePath)

Here you can find the source of deleteParent(String filePath)

Description

Delete source file and parent folder if empty

License

Open Source License

Parameter

Parameter Description
filePath file path includes file name

Exception

Parameter Description
Exception an exception

Declaration

public static void deleteParent(String filePath) throws Exception 

Method Source Code

//package com.java2s;
/*//w  ww .  j ava  2s  . com
 * Copyright (C) 2013-2015 E-Spring Tran
 * 
 *             https://espringtran.com
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;

public class Main {
    /**
     * Delete source file and parent folder if empty
     * 
     * @param filePath
     *            file path includes file name
     * @throws Exception
     */
    public static void deleteParent(String filePath) throws Exception {
        File file = new File(filePath);
        if (file.exists()) {
            file.delete();
        }
        File parent = file.getParentFile();
        while (parent.exists() && parent.isDirectory() && parent.listFiles().length == 0) {
            parent.delete();
            parent = parent.getParentFile();
        }
    }

    /**
     * Delete source file
     * 
     * @param filePath
     *            file path includes file name
     * @throws Exception
     */
    public static void delete(String filePath) throws Exception {
        File file = new File(filePath);
        if (file.exists()) {
            file.delete();
        }
    }
}

Related

  1. deleteJaasFile(String jaasPath)
  2. deleteKey(String path, String key)
  3. deleteMultipleFiles(String filePath, int numberOfFiles)
  4. deleteNonEmptyDirectory(String dirPath)
  5. deleteOldStorageFile(String filepath)
  6. deletePath(File dirPath)
  7. deletePath(File path)
  8. deletePath(final File path)
  9. deletePathRecursive(File path)