zoukankan      html  css  js  c++  java
  • 根据文件原始名称,文件根路径按照日期生成存储路径

    背景

    目前许多项目都要在本地生成文件,一般都要根据 文件的根目录和自定义的文件名称拼接出文件的绝对路径。下面提供一个工具方法供参考


    public class FileUtil {
        private static final DateFormat df = new SimpleDateFormat("yyyyMMdd");
        private static final DateFormat df1 = new SimpleDateFormat("HHmmss");
    
        //根据文件名称,文件根路径按照日期生成存储路径
        public static String generateFilePath(String fileName, String rootFolder) {
            if (StringUtils.isBlank(fileName) || StringUtils.isBlank(rootFolder)) {
                throw new RuntimeException("fileName and rootFolder cannot be null");
            }
    
            Date currentDate = new Date();
            Calendar c = Calendar.getInstance();
            c.setTime(currentDate);
            String fileSystemName = "";
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
    
            StringBuilder fileNameBuilder = new StringBuilder(fileName);
            fileSystemName = fileNameBuilder.insert(fileName.indexOf("."), "_" + uuid).insert(fileName.indexOf("."), "_" + df1.format(currentDate)).toString();
            StringBuffer filePath = new StringBuffer();
            filePath.append(rootFolder).append(File.separator).append(df.format(currentDate));
            File _folder = new File(filePath.toString());
            if (!_folder.exists()) {
                _folder.mkdir();
            }
            filePath.append(File.separator).append(c.get(Calendar.HOUR_OF_DAY));
            _folder = new File(filePath.toString());
            if (!_folder.exists()) {
                _folder.mkdir();
            }
            filePath.append(File.separator).append(fileSystemName);
            return filePath.toString();
        }
    
    }

    测试

  • 相关阅读:
    PHP快速排序算法
    PHP选择排序算法
    php几个常用的概率算法(抽奖、广告首选)
    免费Git客户端:sourcetree详细介绍
    apidoc @apiGroup兼容中文
    PHP中的精确计算bcadd,bcsub,bcmul,bcdiv 及 扩展安装
    mysql-表分区
    mysql表优化
    MySQL执行计划extra中的using index 和 using where using index 的区别
    mysql-锁
  • 原文地址:https://www.cnblogs.com/qingshan-tang/p/12396296.html
Copyright © 2011-2022 走看看