package com.cpic.util;

/**
 * Created by Kane on 2017/3/27.
 */

import java.io.File;
import java.io.IOException;

public class FileTools
{
    private FileTools() {}

    public static void deleteDirectory( String path ) throws IOException
    {
        File dir = new File( path );

        if ( !dir.exists() )
        {
            return;
        }

        //先删除子目录和文件
        File[] subFiles = dir.listFiles();
        int index = 0;

        while ( index < subFiles.length )
        {
            if ( subFiles[index].isDirectory() )
            {
                deleteDirectory( subFiles[index].getAbsolutePath() );
            }

            if ( !subFiles[index].delete() )
            {
                throw new IOException( "目录" + subFiles[index].getAbsolutePath() + "无法删除！" );
            }

            index++;
        }

        dir.delete();
    }
}
