Java File Path Delete DeleteFileFolder(String path)

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

Description

Delete File Folder

License

Apache License

Declaration

public static void DeleteFileFolder(String path) 

Method Source Code

//package com.java2s;
/*/*from   w ww  .  j av  a 2s  . c o  m*/
 * Copyright 2013-2016 Indiana University
 *
 * 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 {
    public static void DeleteFileFolder(String path) {

        File file = new File(path);
        if (file.exists()) {
            do {
                delete(file);
            } while (file.exists());
        } else {
            System.out.println("File or Folder not found : " + path);
        }

    }

    private static void delete(File file) {
        if (file.isDirectory()) {
            String fileList[] = file.list();
            if (fileList.length == 0) {
                System.out
                        .println("Deleting Directory : " + file.getPath());
                file.delete();
            } else {
                int size = fileList.length;
                for (int i = 0; i < size; i++) {
                    String fileName = fileList[i];
                    System.out.println("File path : " + file.getPath()
                            + " and name :" + fileName);
                    String fullPath = file.getPath() + "/" + fileName;
                    File fileOrFolder = new File(fullPath);
                    System.out.println("Full Path :"
                            + fileOrFolder.getPath());
                    delete(fileOrFolder);
                }
            }
        } else {
            System.out.println("Deleting file : " + file.getPath());
            file.delete();
        }
    }
}

Related

  1. deleteFile(String path)
  2. deleteFile(String path, String filename)
  3. deleteFile(String strFolderPath, String strFileName)
  4. deleteFile(String strPath)
  5. deleteFile(String strPath, String fileName)
  6. deleteFileFromDisk(String name, String path)
  7. deleteFileIfExists(String path)
  8. deleteFileOrDirectory(String path)
  9. deleteFileOrDirectory(String path)