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

 

posted on 2019-04-08 11:15  黑狱  阅读(1114)  评论(0编辑  收藏  举报