
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