Here you can find the source of deleteParent(String filePath)
Parameter | Description |
---|---|
filePath | file path includes file name |
Parameter | Description |
---|---|
Exception | an exception |
public static void deleteParent(String filePath) throws Exception
//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(); } } }