package mytest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.TreeSet;
public class CreateFile {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File f2=new File("D:\\mt4");
try {
findSingleFile(f2);//参数是被检测的文件不能带有文件夹
} catch (Exception e) {
e.printStackTrace();
}
}
public static void findSingleFile(File file) throws Exception {
File []files1=file.listFiles();
TreeSet<File> tchongfu=new TreeSet<File>();
int count = 0 ;
for(int x=0;x<files1.length;x++){
for(int y=x+1;y<files1.length;y++){
if(files1[x].length()==files1[y].length()){
synchronized(file.getClass()){
byte[] data1 = new byte[(int) files1[x].length()];
byte[] data2 = new byte[(int) files1[y].length()];
Boolean flag=false;
FileInputStream rfis1 = new FileInputStream(files1[x]);
FileInputStream rfis2 = new FileInputStream(files1[y]);
//分别将两个文件的内容读入缓冲区
rfis1.read(data1);
rfis2.read(data2);
for (int i=0; i<data1.length; i++) {
//只要有一个字节不同,两个文件就不一样
if (data1[i] == data2[i]) {
flag=true;
continue;
}
else{
flag=false;
break;
}
}
if(flag){
count ++;
rfis1.close();
rfis2.close();
tchongfu.add(files1[y]);
}
}
}
}
}
delFile(tchongfu);
}
private static void delFile(TreeSet<File> ts) throws Exception{
Iterator<File> it =ts.iterator();
while(it.hasNext()){
String sss=((File)it.next()).getName();
(new File("D:\\mt4\\" + sss)).delete();
System.out.println(sss);
}
}
}