安卓缓存管理工具类

package tv.buka.roomSdk.util;

import android.content.Context;
import android.os.Environment;
import android.text.format.Formatter;

import java.io.File;

import tv.buka.roomSdk.util.uploadAndDownload.DownloadTask;

/**
 * @说明: //清除缓存、获取缓存文件大小、格式化
 * @作者: hwk
 * @创建日期: 2017/8/24 15:58
 */
public class CacheDataManager {
    /**
     * 清理系统缓存
     *
     * @param context
     */
    public static void clearSystemCache(Context context) {
        try {
            //系统缓存
            deleteDir(context.getCacheDir());
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                deleteDir(context.getExternalCacheDir());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 清理过期缓存文件,启动页调用
     */
    public static void clearPastDueCacheFile() {
        try {
            //在线预览缓存
            File file = new File(Environment.getExternalStorageDirectory() + "/" + ConstantUtil.ROOT_NAME + "/fileCache/");
            deletePastDueFile(file);
            //日历缓存
            File fileLog = new File(Environment.getExternalStorageDirectory() + "/" + "/logger/");
            deletePastDueFile(fileLog);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 先根遍历序递归删除文件夹
     *
     * @param dirFile 要被删除的文件或者目录
     */
    private static void deletePastDueFile(File dirFile) {
        try {
            if (dirFile.exists()) {
                if (dirFile.isFile()) {
                    if (isPastDue(dirFile)) {
                        dirFile.delete();
                    }
                } else {
                    File[] files = dirFile.listFiles();
                    if (files != null && files.length != 0) {
                        for (File file : dirFile.listFiles()) {
                            if (isPastDue(file)) {
                                deletePastDueFile(file);
                            }
                        }
                    }
                    if (isPastDue(dirFile)) {
                        dirFile.delete();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static long pastDueTime = 7 * 24 * 60 * 60 * 1000;//文件过期清理的时间

    /**
     * 判断一个文件、文件夹是否过期
     *
     * @param file
     * @return
     */
    private static boolean isPastDue(File file) {
        try {
            long lastTime = file.lastModified();
            long nowTime = System.currentTimeMillis();
            return (nowTime - lastTime) > pastDueTime;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 删除文件夹
     *
     * @param dir
     * @return
     */
    private static boolean deleteDir(File dir) {
        try {
            if (dir != null && dir.isDirectory()) {
                String[] children = dir.list();
                for (int i = 0; i < children.length; i++) {
                    boolean success = deleteDir(new File(dir, children[i]));
                    if (!success) {
                        return false;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dir.delete();
    }

    /**
     * 获取文件夹大小
     *
     * @param file
     * @return
     */
    public static long getFolderSize(File file) {
        long size = 0;
        try {
            File[] fileList = file.listFiles();
            for (int i = 0; i < fileList.length; i++) {
                // 如果下面还有文件
                if (fileList[i].isDirectory()) {
                    size = size + getFolderSize(fileList[i]);
                } else {
                    size = size + fileList[i].length();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return size;
    }
}
发布了33 篇原创文章 · 获赞 20 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/huangwenkui1990/article/details/94721597