|
@@ -0,0 +1,152 @@
|
|
|
|
+package com.wdkl.ncs.android.lib.utils;
|
|
|
|
+
|
|
|
|
+import android.content.Context;
|
|
|
|
+import android.os.Environment;
|
|
|
|
+import android.support.annotation.NonNull;
|
|
|
|
+import android.text.TextUtils;
|
|
|
|
+import android.util.Log;
|
|
|
|
+
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.FilenameFilter;
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
+import java.util.Date;
|
|
|
|
+
|
|
|
|
+public class DeleteFileRunnable implements Runnable {
|
|
|
|
+
|
|
|
|
+ private Context context;
|
|
|
|
+ private String regEx;
|
|
|
|
+ private String dirPath;
|
|
|
|
+ private boolean prefix;
|
|
|
|
+ private int day;
|
|
|
|
+ private ExeCallBack callBack;
|
|
|
|
+ private static final String TAG = DeleteFileRunnable.class.getSimpleName();
|
|
|
|
+
|
|
|
|
+ public DeleteFileRunnable(Context context, String dirPath, boolean prefix, String regEx, int day, final ExeCallBack callBack) {
|
|
|
|
+ this.context = context;
|
|
|
|
+ this.regEx = regEx;
|
|
|
|
+ this.dirPath = dirPath;
|
|
|
|
+ this.prefix = prefix;
|
|
|
|
+ this.day = day;
|
|
|
|
+ this.callBack = callBack;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void run() {
|
|
|
|
+ //删除文件
|
|
|
|
+ deleteFiles();
|
|
|
|
+ //删除缓存
|
|
|
|
+ //clearAllCache(context);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private void deleteFiles() {
|
|
|
|
+ if (!TextUtils.isEmpty(dirPath)) {
|
|
|
|
+ File dir = new File(dirPath);
|
|
|
|
+ if (dir.exists() && dir.isDirectory()) {
|
|
|
|
+ if (TextUtils.isEmpty(regEx)) {
|
|
|
|
+ //删除所有文件
|
|
|
|
+ deleteDir(dir);
|
|
|
|
+ } else {
|
|
|
|
+ //删除指定前缀或后缀名称文件
|
|
|
|
+ DeleteFileFilter filter = new DeleteFileFilter(prefix, regEx);
|
|
|
|
+
|
|
|
|
+ File[] fileList = dir.listFiles(filter);
|
|
|
|
+ if (fileList != null && fileList.length > 0) {
|
|
|
|
+ for (File file : fileList) {
|
|
|
|
+ if (file.isFile() && file.exists()) {
|
|
|
|
+ if (day > 0) {
|
|
|
|
+ //按时间删除文件
|
|
|
|
+ try {
|
|
|
|
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
|
|
|
+ //获取当前时间
|
|
|
|
+ Date end = dateFormat.parse(dateFormat.format(new Date(System.currentTimeMillis())));
|
|
|
|
+ //文件时间减去当前时间
|
|
|
|
+ Date start = dateFormat.parse(dateFormat.format(new Date(file.lastModified())));
|
|
|
|
+ long diff = end.getTime() - start.getTime();//这样得到的差值是微秒级别
|
|
|
|
+ long days = diff / (1000 * 60 * 60 * 24);
|
|
|
|
+ if (days >= day) {
|
|
|
|
+ boolean delete = file.delete();
|
|
|
|
+ Log.i(TAG, "delete file 111111" + (delete ? " --> success" : "failed"));
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception ex) {
|
|
|
|
+ //
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ boolean delete = file.delete();
|
|
|
|
+ Log.i(TAG, "delete file 222222" + (delete ? " --> success" : "failed"));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (callBack != null) {
|
|
|
|
+ callBack.onFinish();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void clearAllCache(Context context) {
|
|
|
|
+ deleteDir(context.getCacheDir());
|
|
|
|
+ if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
|
|
|
|
+ deleteDir(context.getExternalCacheDir());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private boolean deleteDir(File dir) {
|
|
|
|
+ if (dir != null) {
|
|
|
|
+ if (dir.isDirectory()) {
|
|
|
|
+ String[] children = dir.list();
|
|
|
|
+ for (String child : children) {
|
|
|
|
+ boolean success = deleteDir(new File(dir, child));
|
|
|
|
+ if (!success) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ if (day > 0) {
|
|
|
|
+ //按时间删除文件
|
|
|
|
+ try {
|
|
|
|
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
|
|
|
+ //获取当前时间
|
|
|
|
+ Date end = dateFormat.parse(dateFormat.format(new Date(System.currentTimeMillis())));
|
|
|
|
+ //文件时间减去当前时间
|
|
|
|
+ Date start = dateFormat.parse(dateFormat.format(new Date(dir.lastModified())));
|
|
|
|
+ long diff = end.getTime() - start.getTime();
|
|
|
|
+ long days = diff / (1000 * 60 * 60 * 24);
|
|
|
|
+ if (days >= day) {
|
|
|
|
+ return dir.delete();
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception ex) {
|
|
|
|
+ //
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ return dir.delete();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ class DeleteFileFilter implements FilenameFilter {
|
|
|
|
+
|
|
|
|
+ private boolean prefix;
|
|
|
|
+ private String reg; //文件前缀或后缀
|
|
|
|
+
|
|
|
|
+ public DeleteFileFilter(boolean isPrefix, @NonNull String regEx) {
|
|
|
|
+ this.prefix = isPrefix;
|
|
|
|
+ this.reg = regEx;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public boolean accept(File file, String s) {
|
|
|
|
+ return prefix ? s.startsWith(reg) : s.endsWith(reg);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public interface ExeCallBack {
|
|
|
|
+ void onFinish();
|
|
|
|
+ }
|
|
|
|
+}
|