根据当前日期生成年-月-日多层级文件夹

博客介绍了根据当前日期按年-日-月生成多层级目录的方法。接收文件需按日期区分,将每天接收的文件放在不同文件夹下,还给出了在d盘生成2019/5/2多层级目录文件夹的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

根据当前日期生成多层级目录,按年-日-月来生成。

接收文件需要根据日期接收,然后将每天接收的文件放在不同的文件夹下,好区分。
这是在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);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值