Java File Path Delete deleteFile(File path)

Here you can find the source of deleteFile(File path)

Description

This function will recursively delete directories and files.

License

Open Source License

Parameter

Parameter Description
path File or Directory to be deleted

Return

true indicates success.

Declaration

public static boolean deleteFile(File path) 

Method Source Code


//package com.java2s;
/*// w  ww .  j a  v a  2s . co m
 * #%L
 * Osm2garminAPI
 * %%
 * Copyright (C) 2011 - 2012 Frantisek Mantlik <frantisek at mantlik.cz>
 * %%
 * 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 2 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/gpl-2.0.html>.
 * #L%
 */

import java.io.*;

public class Main {
    /**
     * This function will recursively delete directories and files.
     *
     * @param path File or Directory to be deleted
     * @return true indicates success.
     */
    public static boolean deleteFile(File path) {
        if (path.exists()) {
            if (path.isDirectory()) {
                File[] files = path.listFiles();
                for (int i = 0; i < files.length; i++) {
                    if (files[i].isDirectory()) {
                        if (!deleteFile(files[i])) {
                            try {
                                Thread.sleep(300);
                            } catch (InterruptedException ex) {
                            }
                            deleteFile(files[i]);
                        }
                    } else {
                        if (!files[i].delete()) {
                            try {
                                Thread.sleep(300);
                            } catch (InterruptedException ex) {
                            }
                            files[i].delete();
                        }
                    }
                }
            }
        }
        if (!path.delete()) {
            try {
                Thread.sleep(300);
            } catch (InterruptedException ex) {
            }
            return path.delete();
        } else {
            return true;
        }
    }
}

Related

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