Java图片去重

E盘下新建个mt4文件夹,把要检测的图片放进去,在新建个mt2文件夹用来存放重复图片

package mytest;

import java.io.*;
import java.util.Iterator;
import java.util.TreeSet;



public class CreateFile {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		//File f1=new File("E:\\mt3");
		File f2=new File("E:\\mt4");
		//File f2=new File("E:\\mt2");	
		try {
			long startTime=System.currentTimeMillis();
			//delDoubleFile(f1,f2);
			//findSingleFile(f2);
			findSingleFile(f2,"mt2");//参数一是被检测的文件不能带有文件夹   参数二是重复元素复制到哪个文件夹中
			//reName(f1);
			//reName(f2);
			long endTime=System.currentTimeMillis();
			timechange(endTime-startTime);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
	private static void timechange(long ls) {
		// TODO Auto-generated method stub
		  int second=(int) (ls/1000);
		  int h = 0;
		  int d = 0;
		  int s = 0;
		  int temp = second%3600;
		       if(second>3600){
		         h= second/3600;
		              if(temp!=0){
		         if(temp>60){
		         d = temp/60;
		      if(temp%60!=0){
		         s = temp%60;
		      }
		      }else{
		         s = temp;
		      }
		     }
		    }else{
		        d = second/60;
		     if(second%60!=0){
		        s = second%60;
		     }
		    }

		 
		System.out.println("用时"+h+"时"+d+"分"+s+"秒");
	}
	/*private static synchronized void fileCopyDemo(File file1,File file2,int name) throws Exception{//将两个文件复制到同一个文件夹中
		File objectDir1=new File("E:\\mt2"+"\\"+name);
		objectDir1.mkdir();//创建文件夹
		
		File objectPath1=new File ("E:\\mt2"+"\\"+name+"\\"+file1.getName()+".jpg");
		FileInputStream fis1=new FileInputStream(file1);
		FileOutputStream fos1=new FileOutputStream(objectPath1);
		byte []byt1=new byte[1024];
		int len1=0;
		while((len1=fis1.read(byt1))!=-1){
			fos1.write(byt1);
			fos1.flush();
		}
		fis1.close();
		fos1.close();
		System.out.println(name);
		
		File objectPath2=new File ("E:\\mt2"+"\\"+name+"\\"+file2.getName()+".jpg");
		FileInputStream fis2=new FileInputStream(file2);
		FileOutputStream fos2=new FileOutputStream(objectPath2);
		byte []byt2=new byte[1024];
		int len2=0;
		while((len2=fis2.read(byt2))!=-1){
			fos2.write(byt2);
			fos2.flush();
		}
		fis2.close();
		fos2.close();
		System.out.println(name);
		
	}
	*/
	private static void fileCopyDemo(File file,int name) throws Exception{//按数字重命名
		File objectPath=new File ("E:\\mt2\\"+name+".jpg");
		FileInputStream fis=new FileInputStream(file);
		FileOutputStream fos=new FileOutputStream(objectPath);
		byte []byt=new byte[1024];
		int len=0;
		while((len=fis.read(byt))!=-1){
			fos.write(byt);
			fos.flush();
		}
		fos.close();
		System.out.println(name);
		
	}
	private static void fileCopyDemo(File file,String object) throws Exception{//打印到目标文件地址
		File objectPath=new File ("E:\\"+object+"\\"+file.getName());
		FileInputStream fis=new FileInputStream(file);
		FileOutputStream fos=new FileOutputStream(objectPath);
		byte []byt=new byte[1024];
		int len=0;
		while((len=fis.read(byt))!=-1){
			fos.write(byt);
			fos.flush();
		}
		fos.close();
		fis.close();
		System.out.println("正在将重复元素"+file.getName()+"复制到"+object+"文件夹");
	}

	private static  void findSingleFile(File file1,String copyobjectpath) throws Exception {
		// TODO Auto-generated method stub

		File []files1=file1.listFiles();
		TreeSet tchongfu=new TreeSet();
		TreeSet tdayin=new TreeSet();
		int count=0;
		for(int x=0;x tdayin,String object) throws Exception {
		// TODO Auto-generated method stub
		
		Iterator it =tdayin.iterator();
		while(it.hasNext())
		{
			String f= ((File)it.next()).getName();
			fileCopyDemo((new File("E:\\mt4\\" + f)),object);
		}
		
	}
	private static void delFile(TreeSet ts) throws Exception{
		Iterator it =ts.iterator();
		while(it.hasNext())
		{	String sss=((File)it.next()).getName();
			System.out.println(sss+"删除"+((new File("E:\\mt4\\" + sss)).delete()?"成功":"失败"));
		}
	}
	
	private static int getNumber(File file) {//得到文件个数
		// TODO Auto-generated method stub
		int i=0;
		File []fis=new File[1];
		File []files=file.listFiles();
		for(int x=0;x



你可能感兴趣的:(Java图片去重)