Java File Path Delete deleteFile(String path)

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

Description

Recursive deletion engine, traverses through all sub-directories, attempting to delete every file and directory it comes across.

License

Open Source License

Parameter

Parameter Description
path The directory path to be traversed.

Declaration

public static void deleteFile(String path) throws IOException 

Method Source Code


//package com.java2s;
/*/*from  w w w. ja v  a 2s.  c  o  m*/
 * Copyright (c) 2007-2016 AREasy Runtime
 *
 * This library, AREasy Runtime and API for BMC Remedy AR System, is free software ("Licensed Software");
 * you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either version 2.1 of the License,
 * or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * including but not limited to, the implied warranty of MERCHANTABILITY, NONINFRINGEMENT,
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 */

import java.io.*;

public class Main {
    /**
     * Recursive deletion engine, traverses through all sub-directories,
     * attempting to delete every file and directory it comes across.
     * NOTE: this version doesn't do any security checks, nor does it
     * check for file modes and attempt to change them.
     *
     * @param path The directory path to be traversed.
     */
    public static void deleteFile(String path) throws IOException {
        if (path == null)
            throw new IOException("Input file path is null");
        deleteFile(new File(path));
    }

    /**
     * Recursive deletion engine, traverses through all sub-directories,
     * attempting to delete every file and directory it comes across.
     * NOTE: this version doesn't do any security checks, nor does it
     * check for file modes and attempt to change them.
     *
     * @param file The directory <code>File</code> entity to be traversed.
     */
    public static void deleteFile(File file) throws IOException {
        if (file == null)
            throw new IOException("Input file path is null");

        if (file.isFile()) {
            boolean executed = file.delete();
            if (!executed)
                file.deleteOnExit();
        } else {
            if (file.isDirectory()) {
                File list[] = file.listFiles();

                // Process all files recursively
                for (int ix = 0; list != null && ix < list.length; ix++)
                    deleteFile(list[ix]);

                // now try to delete the directory
                boolean executed = file.delete();
                if (!executed)
                    file.deleteOnExit();
            }
        }
    }
}

Related

  1. deleteFile(String filePathAndName)
  2. deleteFile(String filePathName)
  3. deleteFile(String p_filePath, boolean pb_recursive)
  4. deleteFile(String path)
  5. deleteFile(String path)
  6. deleteFile(String path)
  7. deleteFile(String path)
  8. deleteFile(String path)
  9. deleteFile(String path)