LogFileUtils.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package com.dolin.zap.util;
  2. import android.content.Context;
  3. import android.text.TextUtils;
  4. import android.util.Log;
  5. import java.io.File;
  6. import java.text.ParseException;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Date;
  9. import java.util.Locale;
  10. /**
  11. * @author #Suyghur.
  12. * Created on 2021/05/19
  13. */
  14. public class LogFileUtils {
  15. public static void cleanOverdueLog(Context context, long overdueDayMs) {
  16. File folder = new File(getLogFolderDir(context));
  17. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
  18. File[] files = folder.listFiles();
  19. if (files != null) {
  20. for (File f : files) {
  21. if (f.isDirectory() && isFileOverdue(f, overdueDayMs, format)) {
  22. deleteDirectory(f.getPath());
  23. }
  24. }
  25. }
  26. }
  27. // public static void cleanOverdueLog(String folderPath, long overdueDayMs) {
  28. // File folder = new File(folderPath);
  29. // if (!folder.exists()) {
  30. // return;
  31. // }
  32. // File[] files = folder.listFiles();
  33. // if (files != null) {
  34. // for (File f : files) {
  35. // if (f.isDirectory()) {
  36. // cleanOverdueLog(folderPath, overdueDayMs);
  37. // } else {
  38. // if (isFileOverdue(f, overdueDayMs)) {
  39. // f.delete();
  40. // }
  41. // }
  42. // }
  43. // }
  44. // }
  45. private static boolean isFileOverdue(File file, long overdueDayMs) {
  46. return (System.currentTimeMillis() - file.lastModified()) > overdueDayMs;
  47. }
  48. private static boolean isFileOverdue(File file, long overdueDayMs, SimpleDateFormat format) {
  49. String time = file.getName();
  50. Log.d("dolin_zap", "time : " + time);
  51. try {
  52. Date date = format.parse(time);
  53. if (date != null) {
  54. long ts = date.getTime();
  55. Log.d("dolin_zap", "ts : " + ts);
  56. return (System.currentTimeMillis() - ts) > overdueDayMs;
  57. }
  58. } catch (ParseException e) {
  59. e.printStackTrace();
  60. }
  61. return false;
  62. }
  63. public static String getLogFolderDir(Context context) {
  64. File folder = context.getExternalFilesDir("dolin/zap");
  65. if (folder == null) {
  66. folder = new File(context.getFilesDir(), "dolin/zap");
  67. }
  68. if (!folder.exists()) {
  69. folder.mkdirs();
  70. }
  71. return folder.getAbsolutePath();
  72. }
  73. public static String getLogDir(String folderPath, String date) {
  74. File folder = new File(folderPath + File.separator + date);
  75. if (!folder.exists()) {
  76. folder.mkdirs();
  77. }
  78. return folder.getAbsolutePath();
  79. }
  80. public static int getLogFileNumByDate(String folderPath, String date) {
  81. int num = 0;
  82. File folder = new File(folderPath);
  83. if (!folder.exists()) {
  84. return num;
  85. }
  86. File[] files = folder.listFiles();
  87. if (files == null) {
  88. return num;
  89. } else {
  90. return files.length;
  91. }
  92. }
  93. public static boolean deleteFile(String path) {
  94. if (TextUtils.isEmpty(path)) {
  95. return false;
  96. }
  97. File file = new File(path);
  98. if (file.exists()) {
  99. return file.delete();
  100. }
  101. return false;
  102. }
  103. /**
  104. * 删除文件夹以及所有子目录和文件
  105. */
  106. public static boolean deleteDirectory(String path) {
  107. if (TextUtils.isEmpty(path)) {
  108. return false;
  109. }
  110. boolean flag;
  111. // 如果path不以文件分隔符结尾,自动添加文件分隔符
  112. if (!path.endsWith(File.separator)) {
  113. path = path + File.separator;
  114. }
  115. File dirFile = new File(path);
  116. // 如果dir对应的文件不存在,或者不是一个目录,则退出
  117. if (!dirFile.exists() || !dirFile.isDirectory()) {
  118. return false;
  119. }
  120. flag = true;
  121. // 删除文件夹下的所有文件(包括子目录)
  122. File[] files = dirFile.listFiles();
  123. if (files != null && files.length > 0) {
  124. for (File file : files) {
  125. if (file.isFile()) {
  126. // 删除子文件
  127. flag = deleteFile(file.getAbsolutePath());
  128. } else {
  129. // 删除子目录
  130. flag = deleteDirectory(file.getAbsolutePath());
  131. }
  132. if (!flag) {
  133. break;
  134. }
  135. }
  136. }
  137. if (!flag) {
  138. return false;
  139. }
  140. // 删除当前目录
  141. return dirFile.delete();
  142. }
  143. }