java删除文件夹

想删除本地一个项目目录,结果windows说路径太长,不能删除。于是试了试java删除。一切ok。以后一定要抓紧时间学python。

/**
 * Created by rmiao on 4/21/2016.
 */
public class TestDel {

    public static void main(String[] args) throws IOException {
            String dest = "D:\\perforce\\workspace2\\EPC";
            File file = new File(dest);

            forceDelete(file);


    }

    private static void forceDelete(File file) throws IOException {
        if (file.isDirectory()){
            deleteDirectory(file);
        }else{
            boolean filePresent = file.exists();
            if(!file.delete()){
                if(!filePresent){
                    throw  new FileNotFoundException("File does note exist:"+file);
                }

                String message = "Unable to delete file:"+file;
                throw new IOException(message);

            }else{
                System.out.println("-"+file.getAbsolutePath());
            }

        }
    }
    public static void deleteDirectory(File directory) throws IOException{
        if (directory.exists()){
            cleanDirectory(directory);
        }
        if(!directory.delete()){
            String message = "Unable to delete directory "+directory+".";
            throw  new IOException(message);
        }
    }

    /**
     * Cleans a directory without deleting it.
     *
     * @param directory directory to clean
     * @throws IOException in case cleaning is unsuccessful
     */
    public static void cleanDirectory(File directory) throws IOException {
        if (!directory.exists()) {
            String message = directory + " does not exist";
            throw new IllegalArgumentException(message);
        }

        if (!directory.isDirectory()) {
            String message = directory + " is not a directory";
            throw new IllegalArgumentException(message);
        }

        File[] files = directory.listFiles();
        if (files == null) {  // null if security restricted
            throw new IOException("Failed to list contents of " + directory);
        }

        IOException exception = null;
        for (File file : files) {
            try {
                forceDelete(file);
            } catch (IOException ioe) {
                exception = ioe;
            }
        }

        if (null != exception) {
            throw exception;
        }
    }

}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏余生开发

echarts太阳分布图-饼图来回穿梭

var dom = document.getElementById("container");

1202
来自专栏Golang语言社区

Knapsack problem algorithms for my real-life carry-on knapsack

I'm a nomad and live out of one carry-on bag. This means that the total weight o...

1142
来自专栏MelonTeam专栏

Bitmap 源码阅读笔记

导语: Android 系统上的图片的处理,跟Bitmap 这个类脱不了关系,我们有必要去深入阅读里面的源码,以便在工作中能更好的处理Bitmap相关的问题...

2508
来自专栏Jerry的SAP技术分享

How to update BOL entity property value via ABAP code

Suppose I have one product with ID I042416 which could be found in CRM WebClient...

510
来自专栏我和未来有约会

简练的视图模型 ViewModel

patterns & practices Developer Center 发布了 Unity Application Block 1.2 for Silver...

2189
来自专栏搞前端的李蚊子

Html5模拟通讯录人员排序(sen.js)

// JavaScript Document  var PY_Json_Str = ""; var PY_Str_1 = ""; var PY_Str_...

5916
来自专栏Hadoop数据仓库

Oracle sqlldr 如何导入一个日期列

1. LOAD DATA INFILE * INTO TABLE test FIELDS TERMINATED BY X'9' TRAILING NULLCO...

1796
来自专栏前端儿

Web 前端颜色值--字体--使用,整理整理

颜色值 CSS 颜色使用组合了红绿蓝颜色值 (RGB) 的十六进制 (hex) 表示法进行定义。对光源进行设置的最低值可以是 0(十六进制 00)。最高值是 2...

2322
来自专栏祝威廉

Spark Streaming Dynamic Resource Allocation

DRA has already been implemented since Spark 1.2 . However the existing Spark DR...

1403
来自专栏WOLFRAM

向日葵中的数学之美

1833

扫码关注云+社区