Java File Path Delete deleteAndCreateFolder(String path)

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

Description

delete And Create Folder

License

Apache License

Declaration

public static String deleteAndCreateFolder(String path) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.File;

import java.io.IOException;

public class Main {
    public static String deleteAndCreateFolder(String path) throws IOException {

        boolean success_del;
        File folder = new File(path);
        success_del = deleteDirectory(folder);
        if (!success_del) {
            // Deletion failed
            throw new IOException();
        }/*from w w  w  .ja  va2  s  .c o  m*/
        File newFolder = new File(path);

        if (newFolder.mkdir() == true) {
            return path;
        } else {
            throw new IOException();
        }

    }

    static boolean deleteDirectory(File path) {
        if (path.exists()) {
            File[] files = path.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    deleteDirectory(files[i]);
                } else {
                    files[i].delete();
                }
            }
        }
        return (path.delete());
    }
}

Related

  1. deleteAllFilesIn(String dir)
  2. deleteAllFilesInDir(File dir)
  3. deleteAllFilesInDirectory(String path)
  4. deleteAllFilesOnlyInDirectory(File startingDir)
  5. deleteAllFilesRecursively(String directoryName)
  6. deleteBlankPath(String path)
  7. deleteChildDirectoriesExcept(File path, String... directoriesToSkip)
  8. deleteContents(File dirPath, List failures)
  9. deleteContentsOnly(final String srcPath)