Java删除指定路径下所有文件和文件夹

来源:互联网 发布:淘宝店铺回头率怎么看 编辑:程序博客网 时间:2024/05/24 01:40

这是我在实际的项目中遇见的问题,备份一下代码,以后用到直接拿来用。我就额不细说了,很简单,就是File的操作。直接上代码。我相信可以看明白的。

public void deleteUserDataFolder(String datapath) {        try {            deleteAllFile(datapath);            String filePath = datapath;            filePath = filePath.toString();            File myFilePath = new File(filePath);            myFilePath.delete();        } catch (Exception e) {//日志        }    }    private void deleteAllFile(String datapath) {        File file = new File(datapath);        if (!file.exists()) {            return ;        }        if (!file.isDirectory()) {            return;        }        String[] tempList = file.list();        File temp = null;        for (int i = 0; i < tempList.length; i++) {            if (datapath.endsWith(File.separator)) {                temp = new File(datapath + tempList[i]);            } else {                temp = new File(datapath + File.separator + tempList[i]);            }            if (temp.isFile()) {                temp.delete();            }            if (temp.isDirectory()) {                deleteAllFile(datapath + "/" + tempList[i]);                deleteUserDataFolder(datapath + "/" + tempList[i])            }        }        return;    }
0 0
原创粉丝点击