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

 

转载于:https://www.cnblogs.com/tysk/p/10669390.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值