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 ...
随机推荐
- 自学PHP的正确方法与经验
我是2015年开始接触认识到PHP编程方面的知识,2012年我还是一名刚毕业的大学生开始踏入社会从事自己一份学校推荐的自动化职业,自动化工作枯燥无味,每天基本上3点一线,食堂-公司机器-宿舍,做了3年 ...
- 解决matplotlib中文显示
网上搜的很多方法都不是很好用,这里找到了一个比较好用的办法. 首先将win上的中文字体复制到linux目录下面,我这里使用的是simhei.ttf.然后参考如下代码的使用方式: import matp ...
- Taro开发之城市选择器(带坐标)
要写个城市选择器能返回对应的城市(这里只定义到了地级市),同时返回坐标系,参考了网上资料,下面就看看具体代码吧 import Taro, { Component } from '@tarojs/tar ...
- 手眼标定之相机随动eye-in-hand 示例:handeye_movingcam_calibration
* * This example explains how to use the hand eye calibration for the case where* the camera is atta ...
- SVM-sklearn
目的:1000张数字0-9的手写数字,训练识别手写数字:将其作为32*32的0,1化的数字,随后会将其变为1024列的一个向量 原理:SVM就是把平面的点变为一个空间的点,更好切,核函数就是怎么把他变 ...
- /etc/security/limits.conf的相关说明
今天遇到root账户登录不了的情况,很是郁闷,即使单用户修改了root密码也不能登录. 所以就特意看了一下/etc/security/limits.conf,发现是下面这样的.感觉呗坑了许久.(标红线 ...
- Java框架spring Boot学习笔记(六):Spring Boot事务管理
SpringBoot和Java框架spring 学习笔记(十九):事务管理(注解管理)所讲的类似,使用@Transactional注解便可以轻松实现事务管理.
- Google开源软负载seesaw
https://github.com/google/seesaw ------------------------ 在分布式系统中,负载均衡是非常重要的环节,通过负载均衡将请求派发到网络中的一个或多个 ...
- [translation]The rise of college ‘Grade Forgiveness’
Education 教育 The rise of college 'Grade Forgiveness' /*the rise of -的增加*/大学等级宽恕现象的增加. Universities a ...
- Axis通过方法获取webService请求报文
MessageContext messageContext = _call.getMessageContext(); Message reqMsg = messageContext.getReques ...