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;
} } }
java根据图片创建日期,或最后修改日期重命名的更多相关文章
- Oracle表字段的增加、删除、修改和重命名
本文主要是关于Oracle数据库表中字段的增加.删除.修改和重命名的操作. 增加字段语法:alter table tablename add (column datatype [default val ...
- 【Java】对文件或文件夹进行重命名
在Java中,对文件或文件夹进行重命名是很简单的,因为Java的File类已经封装好renameTo的方法. 修改文件或者文件夹的名字都使用这个方法.例如如下的程序: import java.io.* ...
- 针对Oracle表 列字段的增加、删除、修改以及重命名操作sql
增加字段语法:alter table tablename add (column datatype [default value][null/not null],….); 说明:alter table ...
- Oracle使用——Oracle表字段的增加、删除、修改和重命名
增加字段 语法 alter table tablename add (column datatype [default value][null/not null]); 说明:alter table 表 ...
- shell(2)图片重命名
1:图片重命名 原来的图片名字格式: 改成的图片名字格式: #!/bin/bash #重命名 .png和.jpg #如果原文件的图片名称是从0开始,那么count=:从1开始,那么count= cou ...
- java Swing 图片缓冲机制
java Swing 图片缓冲机制: 参考:http://jorneyr.iteye.com/blog/868858#comments package util; import java.awt.ge ...
- python脚本获取文件的创建于修改日期并计算时间差
由于在计算一个算法的运行时间的时候,需要将文件的创建日期与修改日期读取到,然后计算两者之差,在网上搜索了相关的程序之后,自己又修改了一下,把代码贴在这里,供以后查阅使用,也希望可以帮到其他人. # - ...
- Java日期时间API系列8-----Jdk8中java.time包中的新的日期时间API类的LocalDate源码分析
目录 0.前言 1.TemporalAccessor源码 2.Temporal源码 3.TemporalAdjuster源码 4.ChronoLocalDate源码 5.LocalDate源码 6.总 ...
- Java日期时间API系列11-----Jdk8中java.time包中的新的日期时间API类,使用java8日期时间API重写农历LunarDate
通过Java日期时间API系列7-----Jdk8中java.time包中的新的日期时间API类的优点,java8具有很多优点,现在网上查到的农历转换工具类都是基于jdk7及以前的类写的,下面使用ja ...
随机推荐
- angular的符号
1.括号 {{模板标签}}: 模板标签中的内容会被当作一个表达式展开. [传入名] = ”接收变量名“: 可以把一个值传入组件.输入. (事件名) = “处理函数()”: 响应事件.输出. #视图变量 ...
- CentOS6下4网口绑定双IP
1. 基础信息介绍 4个物理网口分别是:eth0,eth1(集成网卡),eth2,eth3(外置网卡) 其中, 内置网卡eth0和eth1绑定到bond0(192.168.224.2 ...
- (转)医疗IT运维系统
http://www.ewei.com/ask/87.html 含义解释 itil运维管理系统,为用户提供专业的it运维管理,对网络运行的状态.故障.性能等监控,又从业务的视角为管理人员提供综合分析和 ...
- nginx 做数据仓库时,location 404 Not Found,发现找不到要用的数据报:Not Found
背景: 获得远程机器某个目录下的数据文件 方案:使用Nginx配置 1./home/ftp/www/ 下面有images 文件夹,为了访问images下面文件,配置Nginx如下: location ...
- kafka创建会话,报Error while executing topic command : Replication factor: 1 larger than available brokers: 0.
bin/kafka-topics.sh --create --zookeeper es1:2181 --replication-factor 1 --partitions 1 --topic top ...
- ssh登录locale报错:cannot change locale (zh_CN.UTF-8): No such file or directory
一.登录ssh报错: Last :: from 172.28.146.109 -bash: warning: setlocale: LC_ALL: cannot change locale (en_C ...
- 解决phpstudy在 cmd窗口输出 php5 中文显示乱码问题
xampp没事,但切换到phpstudy后发现echo中文变成了乱码 找到解决办法:在cmd里输入 chcp 65001 命令 切换字符编码 chcp 65001 就是换成UTF-8 chcp 93 ...
- UGUI脚本添加Btn回调的方法
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; ...
- jquery的$(selector).each(function(index,element))和$.each(dataresource,function(index,element))的区别
$(selector).each(function(index,element)) 定义和用法 each() 方法规定为每个匹配元素规定运行的函数. $(selector).each(function ...
- The 16th Zhejiang Provincial Collegiate Programming Contest Sponsored(E F G H I)
http://acm.zju.edu.cn/onlinejudge/showContestProblems.do?contestId=392 E:Sequence in the Pocket 思路:从 ...