java根据图片创建日期,或最后修改日期重命名

Wesley13
• 阅读 463

java根据图片创建日期,或最后修改日期重命名 java根据图片创建日期,或最后修改日期重命名

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileSystemView;

public class imgRename {

 /**
 * 将图片名称修改成图片属性的[修改时间]
 * 
 * @author InJavaWeTrust
 */
 public static class ReName {

 public static String TimeMod="ModeTime";//ModeTime或CreateTime

 public static void rename(String PATH, String suffix) {
 File file = new File(PATH);
 File[] files = file.listFiles();
 String newName="";
 System.out.println("------------重命名------------");
int i=0;
 String stri=null;
 SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyymmddhhmmss");
 for (File f : files) {
 stri = new DecimalFormat("0000").format(i);
 if(TimeMod.equals("ModeTime")){
 newName=simpleDateFormat.format(new Date(files[i].lastModified()));
 newName=newName+stri+"."+ suffix;
 }else if(TimeMod.equals("CreateTime")){
 newName=getCreateTime(PATH,suffix)+stri+"."+ suffix;
 }else{
 return ;
 }
 newName=newName.replace(" ", "");
 newName=newName.replace("/", "_");
 newName=newName.replace(":", "_");
 System.out.println(f.getName()+" --> "+newName); 
 //System.out.println(PATH + File.separator + newName); 
 f.renameTo(new File(PATH + File.separator + newName));
 i++;
 }
 }

 public static String getCreateTime(String filePath,String suffix){ 
 String strTime = null; 
 try { 
 Process p = Runtime.getRuntime().exec("cmd /C dir " 
 + filePath 
 + "/tc" ); 
 InputStream is = p.getInputStream(); 
 BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
 String line; 
 while((line = br.readLine()) != null){ 
 if(line.endsWith("."+suffix)){ 
 strTime = line.substring(0,17); 
 break; 
 } 
 } 
 } catch (IOException e) { 
 e.printStackTrace(); 
 } 
 //System.out.println("创建时间 " + strTime); 
 //输出:创建时间 2009-08-17 10:21 
 return strTime;
 } 


 public static void main(String[] args) {

 //获得目标的工作目录
String path = getPath();
 if (path != null) {
 //LinkedHashMap<String, String> map = getFiles(path);
 try {
 readfile(path);
 } catch (FileNotFoundException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 } catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 }

 }

 public static String getPath() {
 String path = null;
 JFileChooser fileChooser = new JFileChooser();
 FileSystemView fsv = FileSystemView.getFileSystemView(); //注意了,这里重要的一句
fileChooser.setCurrentDirectory(fsv.getHomeDirectory());
 fileChooser.setDialogTitle("请选择要命名为创建日期的图片文件夹...");
fileChooser.setApproveButtonText("确定");
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

 int returnVal = fileChooser.showOpenDialog(fileChooser);

 if (returnVal == JFileChooser.APPROVE_OPTION) {
 path = fileChooser.getSelectedFile().getAbsolutePath();//这个就是你选择的文件夹的路径
System.out.println(path);
 return path;
 }
 return null;
 }


 /**
 * 重命名
*/
 /**
 * 读取某个文件夹下的所有文件
*/
public static boolean readfile(String filepath) throws FileNotFoundException, IOException {
 try {

 File file = new File(filepath);
 if (!file.isDirectory()) {
 String fileName = file.getName();
 String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
 /*System.out.println("name=" + file.getName());
 System.out.println("path=" + file.getPath());
 System.out.println("absolutepath=" + file.getAbsolutePath());
 System.out.println("suffix=" + suffix);
 */
 if ((suffix.equals( "jpg") || suffix.equals("jpeg") || suffix.equals("gif") || suffix.equals( "png") || suffix.equals("bmp"))&&!fileName.substring(0, 3).equals("201")) {
 rename(file.getPath(), suffix);
 }

 } else if (file.isDirectory()) {
 //System.out.println("文件夹");
String[] filelist = file.list();
 for (int i = 0; i < filelist.length; i++) {
 File readfile = new File(filepath + "\\" + filelist[i]);
 if (!readfile.isDirectory()) {
 String fileName = readfile.getName();
 String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
 System.out.println("name=" + file.getName());
 //System.out.println("path=" + readfile.getPath());
 //System.out.println("absolutepath=" + readfile.getAbsolutePath());
 System.out.println("suffix=" + suffix);

 if ((suffix.equals("jpg") || suffix.equals("jpeg") || suffix.equals("gif") || suffix.equals( "png") || suffix.equals("bmp"))&&!fileName.substring(0, 3).equals("201")) {
 rename(file.getPath(), suffix);
 break;
 }
 } else if (readfile.isDirectory()) {
 readfile(filepath + "\\" + filelist[i]);
 }
 }

 }

 } catch (FileNotFoundException e) {
 System.out.println("readfile() Exception:" + e.getMessage());
 }
 return true;
 }

 }

}

View Code

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Karen110 Karen110
2年前
一篇文章带你了解JavaScript日期
日期对象允许您使用日期(年、月、日、小时、分钟、秒和毫秒)。一、JavaScript的日期格式一个JavaScript日期可以写为一个字符串:ThuFeb02201909:59:51GMT0800(中国标准时间)或者是一个数字:1486000791164写数字的日期,指定的毫秒数自1970年1月1日00:00:00到现在。1\.显示日期使用
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Stella981 Stella981
2年前
Android So动态加载 优雅实现与原理分析
背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我
Stella981 Stella981
2年前
HIVE 时间操作函数
日期函数UNIX时间戳转日期函数: from\_unixtime语法:   from\_unixtime(bigint unixtime\, string format\)返回值: string说明: 转化UNIX时间戳(从19700101 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式举例:hive   selec
Wesley13 Wesley13
2年前
Java 9 被无情抛弃,Java 8 直接升级到 Java 10!!
!(https://oscimg.oschina.net/oscnet/3e08a942dd884e9ab82b63a1f3c4aada.jpg"未命名文件.jpg")Java技术栈不可错过的Java 技术公众号!(https://oscimg.oschina.net/oscnet/00fcff52518e
Wesley13 Wesley13
2年前
Java日期时间API系列35
  通过Java日期时间API系列1Jdk7及以前的日期时间类(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fwww.cnblogs.com%2Fxkzhangsanx%2Fp%2F12032719.html)中得知,Java8以前除了java.sql.Timestamp扩充
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Hacker News 简讯 2020
!(https://oscimg.oschina.net/oscnet/up3b137e2e6620f7a63f11a96485b1fb3b.png)最后更新时间:2020082623:00
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这