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;
} } }

java根据图片创建日期,或最后修改日期重命名的更多相关文章

  1. Oracle表字段的增加、删除、修改和重命名

    本文主要是关于Oracle数据库表中字段的增加.删除.修改和重命名的操作. 增加字段语法:alter table tablename add (column datatype [default val ...

  2. 【Java】对文件或文件夹进行重命名

    在Java中,对文件或文件夹进行重命名是很简单的,因为Java的File类已经封装好renameTo的方法. 修改文件或者文件夹的名字都使用这个方法.例如如下的程序: import java.io.* ...

  3. 针对Oracle表 列字段的增加、删除、修改以及重命名操作sql

    增加字段语法:alter table tablename add (column datatype [default value][null/not null],….); 说明:alter table ...

  4. Oracle使用——Oracle表字段的增加、删除、修改和重命名

    增加字段 语法 alter table tablename add (column datatype [default value][null/not null]); 说明:alter table 表 ...

  5. shell(2)图片重命名

    1:图片重命名 原来的图片名字格式: 改成的图片名字格式: #!/bin/bash #重命名 .png和.jpg #如果原文件的图片名称是从0开始,那么count=:从1开始,那么count= cou ...

  6. java Swing 图片缓冲机制

    java Swing 图片缓冲机制: 参考:http://jorneyr.iteye.com/blog/868858#comments package util; import java.awt.ge ...

  7. python脚本获取文件的创建于修改日期并计算时间差

    由于在计算一个算法的运行时间的时候,需要将文件的创建日期与修改日期读取到,然后计算两者之差,在网上搜索了相关的程序之后,自己又修改了一下,把代码贴在这里,供以后查阅使用,也希望可以帮到其他人. # - ...

  8. Java日期时间API系列8-----Jdk8中java.time包中的新的日期时间API类的LocalDate源码分析

    目录 0.前言 1.TemporalAccessor源码 2.Temporal源码 3.TemporalAdjuster源码 4.ChronoLocalDate源码 5.LocalDate源码 6.总 ...

  9. Java日期时间API系列11-----Jdk8中java.time包中的新的日期时间API类,使用java8日期时间API重写农历LunarDate

    通过Java日期时间API系列7-----Jdk8中java.time包中的新的日期时间API类的优点,java8具有很多优点,现在网上查到的农历转换工具类都是基于jdk7及以前的类写的,下面使用ja ...

随机推荐

  1. MySQL自增列锁模式 innodb_autoinc_lock_mode不同参数下性能测试

    对于innodb_autoinc_lock_mode 各种参数的值的含义,网上也有各种详解,看完觉得意犹未尽,这里不做阐述,只动手测试,看看性能上,到底有没有理论上所说的差别.对于自增列的锁定,据说是 ...

  2. zookeeper学习笔记记录

    zookeeper的概述: ZooKeeper 本质上是一个分布式的小文件存储系统.提供基于类似于文件系统的目录树方式的数据存储,并且可以对树中的节点进行有效管理.从而用来维护和监控你存储的数据的状态 ...

  3. windows下consul利用json文件注册服务

    windows下,以开发模式启动consul命令 consul agent -dev -config-dir=D:\tools\consul 人工注册服务,新建一个json文件 ,放到D:\tools ...

  4. Taro开发小程序移动地图固定中间获取地址

    效果图如下: 绿色地标固定在中间,移动地图的时候获取对应信息. 先定义map. <Map className="location" id={location} latitud ...

  5. 解题(IdenticalTree--拓扑结构相同子树 )

    题目描述 对于两棵彼此独立的二叉树A和B,请编写一个高效算法,检查A中是否存在一棵子树与B树的拓扑结构完全相同. 给定两棵二叉树的头结点A和B,请返回一个bool值,代表A中是否存在一棵同构于B的子树 ...

  6. 【笔记】Python基础六:模块module介绍及常用模块

    一,module模块和包的介绍 1,在Python中,一个.py文件就称之为一个模块(Module). 2,使用模块的好处? 最大的好处是大大提高了代码的可维护性 其次,编写代码不必从零开始,我们编写 ...

  7. [转]使用screw plus来保护php代码安全

    原文链接 https://www.jianshu.com/p/f6425e2f8643 https://github.com/del-xiong/screw-plus http://git.oschi ...

  8. 修改Windows server 时间同步

    1.关闭“与Internet时间同步”选项. 2.禁用Windows时间服务,并将其设置为手动. 3.禁用Hyper-v时间同步服务,并将其设置为手动,这个在Hyper-v软件上选中要修改的虚拟机,设 ...

  9. HttpSession原理及Session冲突

    一.摘要         本文讨论了web服务器靠session id识别客户端.以及透过原理分析session冲突的原因,发现session冲突的原因是保存session id信息的cookie发生 ...

  10. I/O多路复用之 epoll 详解

    1,epoll 原理(POLLIN,POLLOUT 状态): https://blog.csdn.net/hdutigerkin/article/details/7517390 https://blo ...