博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
图片文件重命名
阅读量:7080 次
发布时间:2019-06-28

本文共 3538 字,大约阅读时间需要 11 分钟。

  今天收到一个任务,将所有图片进行重命名,于是就有了下面的函数,循环遍历文件夹进行文件重命名.

1     private static void uploadImageFile(File file) { 2         File[] imageList = file.listFiles(); 3         // 循环遍历文件 4         for (File picFile : imageList) { 5             if (picFile.isFile()) { 6                 String fileName = picFile.getName(); 7                 String suffix = fileName.substring(fileName.lastIndexOf(".")); 8                 String order = new StringBuilder().append(prefix + ((int)(Math.random() * 9000 + 1000))).append(suffix) 9                         .toString();10                 File renameFile = new File(file + "\\" + order);11                 picFile.renameTo(renameFile);12                 System.out.println(picFile.getName());13             }else if(picFile.isDirectory()){14                 uploadImageFile(picFile);15             }16             17         }18     }

 按照随机身份证格式,对图片进行重命名完整类

1 public class RenamePic { 2      3     /** 4      * 测试主方法 5      * @author fgq 2017年7月13日 下午4:48:56 6      * @param args 7      */ 8     public static void main(String[] args) { 9          String path = "F:\\fgq_test";10          File file = new File(path);11          long startTime = System.currentTimeMillis();12          cycleDealFile(file);13          System.out.println(System.currentTimeMillis() - startTime);14     15     }16     17     /**18      * 循环处理文件19      * @author fgq 2017年7月13日 下午4:48:3520      * @param file21      */22     private static void cycleDealFile(File file) {23         File[] imageList = file.listFiles();        24         // 循环遍历文件25         for (File fileItem : imageList) {26             if (fileItem.isFile()) {27                 renameFile(fileItem);28             }else if(fileItem.isDirectory()){29                 cycleDealFile(fileItem);30             }31         }32     }33     34     /**35      * 文件重命名36      * @author fgq 2017年7月13日 下午4:47:4237      * @param file38      */39     private static void renameFile(File file){40         String fileName = file.getName();41         String suffix = fileName.substring(fileName.lastIndexOf("."));42         String order = new StringBuilder().append(generalId()).append(suffix)43                 .toString();44         File renameFile = new File(file.getParentFile() + "\\" + order);45         file.renameTo(renameFile);46     }47     48     /**49      * 生成伪身份证50      * @author fgq 2017年7月13日 下午4:47:1451      * @return52      */53     private static String generalId() {54         55         int areaCode = getRandNum(100000, 999999);56         int yearNum = getRandNum(1600, 2017);57         58         int monthNum = getRandNum(1, 12);59         String monthStr = String.valueOf(monthNum);60         if (Integer.parseInt(monthStr) < 10) {61             monthStr = "0" + monthNum;62         }63         64         int dayNum = getRandNum(1, 30);65         String dayStr = String.valueOf(dayNum);66         if (Integer.parseInt(dayStr) < 10) {67             dayStr = "0" + dayNum;68         }69         int suffix = getRandNum(1000, 9999);70         71         return new StringBuilder().append(areaCode).append(yearNum).append(monthStr).append(dayStr).append(suffix).toString();72     }73     74     /**75      * 获取范围内的随机数76      * @author fgq 2017年7月13日 下午4:46:5577      * @param min78      * @param max79      * @return80      */81     private static int getRandNum(int min, int max) {82         int randNum = min + (int)(Math.random() * ((max - min) + 1));83         return randNum;84     }    85 }
View Code

 

转载于:https://www.cnblogs.com/fxust/p/7159987.html

你可能感兴趣的文章
老李分享:JDK,JRE,JVM区别与联系 1
查看>>
CentOS 7 上systemctl 的用法
查看>>
Android Design框架
查看>>
[Linux] 在 Linux CLI 使用 ssh-keygen 生成 RSA 密钥
查看>>
在 Ubuntu 16.04 Server 上安装 Zabbix
查看>>
Netgear wndr3700v2 路由器刷OpenWrt打造全能服务器(九)ftp服务
查看>>
oracle 存储过程的基本语法
查看>>
程序员应该遵守的编程原则
查看>>
各操作系统配置NTP
查看>>
使用mysql索引技巧及注意事项
查看>>
按照发起的方式,DDoS可以简单分为三类
查看>>
2月新书,送出一本你爱的!
查看>>
2018LinuxCon,开源界的大咖们来了,赶紧行动!
查看>>
10月24日程序员关爱日
查看>>
python函数定义
查看>>
服务器的安装
查看>>
如何优雅的处理异常(java)?
查看>>
VRRP 虚拟冗余路由协议
查看>>
express不是内部或外部命令
查看>>
通过反射获取成员方法并使用
查看>>