删除某个目录下所有文件

/**
     * 删除目录下所有文件
     */
    public static long deleteFile(File file) {
        long cacheLength = -1;
        if (file.isDirectory()) {
            File[] childFiles = file.listFiles();
            if (childFiles == null || childFiles.length == 0) {
                file.delete();
                return cacheLength;
            }
            for (int i = 0; i < childFiles.length; i++) {
                if (childFiles[i].exists()) {
                    long fileLength = 0;
                    fileLength = childFiles[i].length();
                    boolean result = childFiles[i].delete();
                    if (result && fileLength > 0) {
                        cacheLength = cacheLength + fileLength;
                    }
                }
            }
        }
        return cacheLength;
    }

你可能感兴趣的:(Android)