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

1import java.io.BufferedReader; 2import java.io.File; 3import java.io.FileNotFoundException; 4import java.io.IOException; 5import java.io.InputStream; 6import java.io.InputStreamReader; 7import java.text.DecimalFormat; 8import java.text.SimpleDateFormat; 9import java.util.Date; 10import javax.swing.JFileChooser; 11import javax.swing.filechooser.FileSystemView; 12 13public class imgRename { 14 15 /** 16 * 将图片名称修改成图片属性的[修改时间] 17 * 18 * @author InJavaWeTrust 19 */ 20 public static class ReName { 21 22 public static String TimeMod="ModeTime";//ModeTime或CreateTime 23 24 public static void rename(String PATH, String suffix) { 25 File file = new File(PATH); 26 File[] files = file.listFiles(); 27 String newName=""; 28 System.out.println("------------重命名------------"); 29int i=0; 30 String stri=null; 31 SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyymmddhhmmss"); 32 for (File f : files) { 33 stri = new DecimalFormat("0000").format(i); 34 if(TimeMod.equals("ModeTime")){ 35 newName=simpleDateFormat.format(new Date(files[i].lastModified())); 36 newName=newName+stri+"."+ suffix; 37 }else if(TimeMod.equals("CreateTime")){ 38 newName=getCreateTime(PATH,suffix)+stri+"."+ suffix; 39 }else{ 40 return ; 41 } 42 newName=newName.replace(" ", ""); 43 newName=newName.replace("/", "_"); 44 newName=newName.replace(":", "_"); 45 System.out.println(f.getName()+" --> "+newName); 46 //System.out.println(PATH + File.separator + newName); 47 f.renameTo(new File(PATH + File.separator + newName)); 48 i++; 49 } 50 } 51 52 public static String getCreateTime(String filePath,String suffix){ 53 String strTime = null; 54 try { 55 Process p = Runtime.getRuntime().exec("cmd /C dir " 56 + filePath 57 + "/tc" ); 58 InputStream is = p.getInputStream(); 59 BufferedReader br = new BufferedReader(new InputStreamReader(is)); 60 String line; 61 while((line = br.readLine()) != null){ 62 if(line.endsWith("."+suffix)){ 63 strTime = line.substring(0,17); 64 break; 65 } 66 } 67 } catch (IOException e) { 68 e.printStackTrace(); 69 } 70 //System.out.println("创建时间 " + strTime); 71 //输出:创建时间 2009-08-17 10:21 72 return strTime; 73 } 74 75 76 public static void main(String[] args) { 77 78 //获得目标的工作目录 79String path = getPath(); 80 if (path != null) { 81 //LinkedHashMap<String, String> map = getFiles(path); 82 try { 83 readfile(path); 84 } catch (FileNotFoundException e) { 85 // TODO Auto-generated catch block 86 e.printStackTrace(); 87 } catch (IOException e) { 88 // TODO Auto-generated catch block 89 e.printStackTrace(); 90 } 91 } 92 93 } 94 95 public static String getPath() { 96 String path = null; 97 JFileChooser fileChooser = new JFileChooser(); 98 FileSystemView fsv = FileSystemView.getFileSystemView(); //注意了,这里重要的一句 99fileChooser.setCurrentDirectory(fsv.getHomeDirectory()); 100 fileChooser.setDialogTitle("请选择要命名为创建日期的图片文件夹..."); 101fileChooser.setApproveButtonText("确定"); 102fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 103 104 int returnVal = fileChooser.showOpenDialog(fileChooser); 105 106 if (returnVal == JFileChooser.APPROVE_OPTION) { 107 path = fileChooser.getSelectedFile().getAbsolutePath();//这个就是你选择的文件夹的路径 108System.out.println(path); 109 return path; 110 } 111 return null; 112 } 113 114 115 /** 116 * 重命名 117*/ 118 /** 119 * 读取某个文件夹下的所有文件 120*/ 121public static boolean readfile(String filepath) throws FileNotFoundException, IOException { 122 try { 123 124 File file = new File(filepath); 125 if (!file.isDirectory()) { 126 String fileName = file.getName(); 127 String suffix = fileName.substring(fileName.lastIndexOf(".") + 1); 128 /*System.out.println("name=" + file.getName()); 129 System.out.println("path=" + file.getPath()); 130 System.out.println("absolutepath=" + file.getAbsolutePath()); 131 System.out.println("suffix=" + suffix); 132 */ 133 if ((suffix.equals( "jpg") || suffix.equals("jpeg") || suffix.equals("gif") || suffix.equals( "png") || suffix.equals("bmp"))&&!fileName.substring(0, 3).equals("201")) { 134 rename(file.getPath(), suffix); 135 } 136 137 } else if (file.isDirectory()) { 138 //System.out.println("文件夹"); 139String[] filelist = file.list(); 140 for (int i = 0; i < filelist.length; i++) { 141 File readfile = new File(filepath + "\\" + filelist[i]); 142 if (!readfile.isDirectory()) { 143 String fileName = readfile.getName(); 144 String suffix = fileName.substring(fileName.lastIndexOf(".") + 1); 145 System.out.println("name=" + file.getName()); 146 //System.out.println("path=" + readfile.getPath()); 147 //System.out.println("absolutepath=" + readfile.getAbsolutePath()); 148 System.out.println("suffix=" + suffix); 149 150 if ((suffix.equals("jpg") || suffix.equals("jpeg") || suffix.equals("gif") || suffix.equals( "png") || suffix.equals("bmp"))&&!fileName.substring(0, 3).equals("201")) { 151 rename(file.getPath(), suffix); 152 break; 153 } 154 } else if (readfile.isDirectory()) { 155 readfile(filepath + "\\" + filelist[i]); 156 } 157 } 158 159 } 160 161 } catch (FileNotFoundException e) { 162 System.out.println("readfile() Exception:" + e.getMessage()); 163 } 164 return true; 165 } 166 167 } 168 169}

View Code

点赞
收藏

评论区

加载中...

相关推荐

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

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

一篇文章带你了解JavaScript日期

日期对象允许您使用日期(年、月、日、小时、分钟、秒和毫秒)。一、JavaScript的日期格式一个JavaScript日期可以写为一个字符串:ThuFeb02201909:59:51GMT0800(中国标准时间)或者是一个数字:1486000791164写数字的日期,指定的毫秒数自1970年1月1日00:00:00到现在。1\.显示日期使用

Android So动态加载 优雅实现与原理分析

背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我