java建立年月日三級目錄的文件夾

最近做一個統計系統,需要對程序的運行狀態進行打點記錄,仔細一想這玩意如果五分鐘記錄一次狀態的話,需要存好多記錄了,而記錄又不能隨便存放,所以就想到對目錄按照年月日進行分級維護,寫個代碼生成一下這個年月日的分級目錄。

有圖有真相,具體長這個樣子:

代碼如下

public class SimTest {
    @Test
    public void createDir() throws Exception {
        String basePath = "F:\\try";
        createDateDir(basePath);
    }

    public static void createDateDir(String basePath) throws Exception {
        String dayStr = DateUtil.format("yyyy-MM-dd", System.currentTimeMillis());
        String[] dayArr = dayStr.split("-");

        String year = dayArr[0];
        String month = dayArr[1];
        String day = dayArr[2];
        
        String yearDir = basePath + File.separator + year;
        File yearFile = new File(yearDir);
        if (!yearFile.exists()) {
            yearFile.mkdirs();
        }

        String monthDir = yearDir + File.separator + month;
        File monthFile = new File(monthDir);
        if (!monthFile.exists()) {
            monthFile.mkdirs();
        }

        String dayDir = monthDir + File.separator + day;
        File dayFile = new File(dayDir);
        if (!dayFile.exists()) {
            dayFile.mkdirs();
        }
    }
}

感恩地址,一切隨緣

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章