根據當前日期生成年-月-日多層級文件夾

根據當前日期生成多層級目錄,按年-日-月來生成。

接收文件需要根據日期接收,然後將每天接收的文件放在不同的文件夾下,好區分。
這是在d盤下生成的2019/5/2的多層級目錄文件夾
在這裏插入圖片描述

方法
/**
     * @description: TODO 默認創建多層日期 年-月-日 分層文件夾,默認創建到d盤下
     * @param ${null}
     * @return ${url路徑}
     * @throws
     * @author 
     * @date 2019/4/30 21:16
     */
    public static String getDatePathByCreateFile(){
        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateInstance();
        String time = dateFormat.format(date);

        //System.out.println("獲取到精確到日的時間格式爲"+time);
        String[] str = time.split("-");//根據‘-’進行拆分字符串 拆分出來的日期有,年,日,月,根據年月日創建文件夾
        //System.out.println("當前年份爲"+str[0]);
        //System.out.println("當前月份爲:"+str[1]);
        //System.out.println("當前的日爲:"+str[2]);
        //System.out.println("------------------------文件寫入-------------------------");
        //創建文件夾
        String filePath = "d:/"+str[0]+"/"+str[1]+"/"+str[2];
        File file = new File(filePath);
        boolean b = file.mkdirs();//這個方法的含義時即使抽象的父目錄不存在,也會創建出不存在的抽象父目錄
        if(b){
            System.out.println("不存在,已創建!");
        }else{
            System.out.println("存在,沒有創建!");
        }
        //System.out.println("---------------------------文件寫入結束---------------------------");
        return filePath;
    }
    /**
     * @description: TODO 根據參數dish也就是盤符創建進哪個文件夾
     * @param ${dish 盤符, c盤就填c,d盤就填d}
     * @return ${return_type}
     * @throws
     * @author 
     * @date 2019/4/30 21:27
     */

    public static String getDatePathByCreateFile(String dish){
        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateInstance();
        String time = dateFormat.format(date);

        //System.out.println("獲取到精確到日的時間格式爲"+time);
        String[] str = time.split("-");//根據‘-’進行拆分字符串 拆分出來的日期有,年,日,月,根據年日月創建文件夾
        //System.out.println("當前年份爲"+str[0]);
        //System.out.println("當前月份爲:"+str[1]);
        //System.out.println("當前的日爲:"+str[2]);
        //System.out.println("------------------------文件寫入-------------------------");
        //創建文件夾
        String filePath = dish+":/"+str[0]+"/"+str[1]+"/"+str[2];
        File file = new File(filePath);
        if(file.exists()){
            System.out.println("文件已存在");
        }else {
            boolean b = file.mkdirs();//這個方法的含義時即使抽象的父目錄不存在,也會創建出不存在的抽象父目錄
            if (b) {
                System.out.println("不存在,已創建!");
            } else {
                System.out.println("存在,不需要創建!");
            }
            //System.out.println("---------------------------文件寫入結束---------------------------");
        }
        return filePath;
    }
     public static void main(String[] args) {
        //在c盤下創建一個年月日多層級目錄
        String path = getDatePathByCreateFile("c");
        System.out.println(path);
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章