Automatically delete log files older than seven days

Many people have added log management systems to their projects, some of which are written by themselves, and some are dependent libraries written by others, but not all dependent libraries can meet our needs. At this time, do we have to do it ourselves? add it up

Background, log management has been added to the project I took over, but there is no regular cleaning function. I think there are even files from two years ago. One txt file a day is not big, but there is a lot of it, so it is still necessary to add regularly cleaned

Above code:

/**
 * Created by Forrest.
 * User: Administrator
 * Date: 2020/12/11
 * Description:
 */
public class DelFile {
    public static void del(Context context) {
        String path = "/sdcard/dhypda/logs/";
        File file = new File(path);
        File[] files = file.listFiles();// 读取
        getFileName(files, context);
    }

    private static void getFileName(File[] files, Context context) {
        if (files != null) {// 先判断目录是否为空,否则会报空指针
            for (File file : files) {
                if (file.isDirectory()) {
                    getFileName(file.listFiles(), context);
                } else {
                    String fileName = file.getName();
                    if (fileName.endsWith(".txt")) {
                        try {
                            String s = fileName.substring(0, fileName.lastIndexOf(".")).toString();
                            String ph = "/sdcard/dhypda/logs/" + "" + s.trim() + ".txt";
                            File fe = new File(ph);
                            Log.i("test", ph + "/" + s.trim() + "/" + getStringToday() + "/" + daysBetween(s.trim(), getStringToday()));
                            if (daysBetween(s.trim(), getStringToday()) >= 7) {
                                fe.delete();
                            }
                        } catch (ParseException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }// 当前时间
                    }
                }
            }
        }
    }

    /**
     * 得到现在时间
     *
     * @return 字符串 yyyyMMdd HHmmss
     */
    public static String getStringToday() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * 字符串的日期格式的计算
     */
    public static int daysBetween(String smdate, String bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(smdate));
        long time1 = cal.getTimeInMillis();
        cal.setTime(sdf.parse(bdate));
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(between_days));
    }
}

Just use it directly

The principle is very simple

1. Find the directory where we store the logs

2. Find the file with the suffix txt

3Because my log is named in yyyyMMdd format, it is ok to directly calculate the time difference between the current time and the time of the file name

4 Delete files that exceed the time based on the time difference

just sauce~

Guess you like

Origin blog.csdn.net/lanrenxiaowen/article/details/111036285