Java File Path Delete delete(String path)

Here you can find the source of delete(String path)

Description

Delete the file or directory at the supplied path.

License

Apache License

Parameter

Parameter Description
path the path to the file or directory that is to be deleted

Return

true if the file or directory at the supplied path existed and was successfully deleted, or false otherwise

Declaration

public static boolean delete(String path) 

Method Source Code

//package com.java2s;
/*//from   ww w .j  ava  2 s  .co  m
 * ModeShape (http://www.modeshape.org)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;

public class Main {
    /**
     * Delete the file or directory at the supplied path. This method works on a directory that is not empty, unlike the
     * {@link File#delete()} method.
     * 
     * @param path the path to the file or directory that is to be deleted
     * @return true if the file or directory at the supplied path existed and was successfully deleted, or false otherwise
     */
    public static boolean delete(String path) {
        if (path == null || path.trim().length() == 0)
            return false;
        return delete(new File(path));
    }

    /**
     * Delete the file or directory given by the supplied reference. This method works on a directory that is not empty, unlike
     * the {@link File#delete()} method.
     * 
     * @param fileOrDirectory the reference to the Java File object that is to be deleted
     * @return true if the supplied file or directory existed and was successfully deleted, or false otherwise
     */
    public static boolean delete(File fileOrDirectory) {
        if (fileOrDirectory == null)
            return false;
        if (!fileOrDirectory.exists())
            return false;

        // The file/directory exists, so if a directory delete all of the contents ...
        if (fileOrDirectory.isDirectory()) {
            File[] files = fileOrDirectory.listFiles();
            if (files != null) {
                for (File childFile : files) {
                    delete(childFile); // recursive call (good enough for now until we need something better)
                }
            }
            // Now an empty directory ...
        }
        // Whether this is a file or empty directory, just delete it ...
        return fileOrDirectory.delete();
    }
}

Related

  1. delete(String filePath)
  2. delete(String filePath)
  3. delete(String filePath, boolean recursive)
  4. delete(String path)
  5. delete(String path)
  6. delete(String path)
  7. deleteAll(File path)
  8. deleteAll(File path)
  9. deleteAll(File path)