当前位置: 首页 > news >正文

图片文件重命名

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

 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:35
20      * @param file
21      */
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:42
37      * @param file
38      */
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:14
51      * @return
52      */
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:55
77      * @param min
78      * @param max
79      * @return
80      */
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

相关文章:

  • Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)
  • 产品和团队
  • MySQL慎用 ENUM 字段
  • mysql取差集、交集、并集
  • Tex: The top-level auxiliary file: *.aux I couldn't open style file IEEEtran.bst 解决方法
  • 【java项目实战】一步步教你使用MyEclipse搭建java Web项目开发环境(一)
  • 嵌入式开发之hisilicon---hi3536 处理器简介
  • 分布式开放消息系统(RocketMQ)的原理与实践
  • 【翻译】Mashape是如何管理15000个API和微服务的(三)
  • nodeJS中的包
  • oracle学习3
  • 即将到来的Android N,将具备这些新特性
  • 三层架构—简析
  • kibana 常用查询方法
  • 利用linux shell自己主动顶贴
  • 自己简单写的 事件订阅机制
  • Angular 响应式表单 基础例子
  • Docker 1.12实践:Docker Service、Stack与分布式应用捆绑包
  • gulp 教程
  • JS创建对象模式及其对象原型链探究(一):Object模式
  • log4j2输出到kafka
  • Protobuf3语言指南
  • Spring Security中异常上抛机制及对于转型处理的一些感悟
  • Vue--数据传输
  • 初识 beanstalkd
  • 浮现式设计
  • 机器学习中为什么要做归一化normalization
  • 技术攻略】php设计模式(一):简介及创建型模式
  • 扑朔迷离的属性和特性【彻底弄清】
  • 七牛云假注销小指南
  • 前端之React实战:创建跨平台的项目架构
  • 深度解析利用ES6进行Promise封装总结
  • 世界上最简单的无等待算法(getAndIncrement)
  • 数据可视化之 Sankey 桑基图的实现
  • 数据库写操作弃用“SELECT ... FOR UPDATE”解决方案
  • 职业生涯 一个六年开发经验的女程序员的心声。
  • ​MySQL主从复制一致性检测
  • #微信小程序(布局、渲染层基础知识)
  • (二)hibernate配置管理
  • (图)IntelliTrace Tools 跟踪云端程序
  • (一)Mocha源码阅读: 项目结构及命令行启动
  • (一)RocketMQ初步认识
  • * CIL library *(* CIL module *) : error LNK2005: _DllMain@12 already defined in mfcs120u.lib(dllmodu
  • ******IT公司面试题汇总+优秀技术博客汇总
  • .jks文件(JAVA KeyStore)
  • .NET CORE Aws S3 使用
  • .net core控制台应用程序初识
  • .NET MVC之AOP
  • .net实现头像缩放截取功能 -----转载自accp教程网
  • .NET委托:一个关于C#的睡前故事
  • /bin/rm: 参数列表过长"的解决办法
  • [.net] 如何在mail的加入正文显示图片
  • [2021]Zookeeper getAcl命令未授权访问漏洞概述与解决
  • [AutoSar]BSW_Com02 PDU详解
  • [BZOJ1060][ZJOI2007]时态同步 树形dp