import java.io.*;import java.util.*;public class FindDuplicatedFile { //fileList is the list of files in this directory static private ListfileList = new ArrayList<>(); static private void getFileList(String filePath) throws NotDirectoryExpectation{ File file = new File(filePath); if(!file.isDirectory()){ throw new NotDirectoryExpectation(); } File[] fileArray = file.listFiles(); if(fileArray == null) return; for(int i = 0; i getDuplicatedFile(String filePath) throws NotDirectoryExpectation{ getFileList(filePath); List duplicateFiles = new ArrayList<>(); for(int i = 0; i< fileList.size(); i++){ File temp = fileList.get(i); for(int j = i+1; j < fileList.size(); j++){ if(fileList.get(j).getName().equals(temp.getName()) && fileList.get(j).length() == temp.length()) { duplicateFiles.add(fileList.get(j)); duplicateFiles.add(temp); } } } return duplicateFiles; } public static void printDuplicates(List list){ Iterator it = list.iterator(); if(list.size() == 0){ System.out.println("no Duplicated File!"); } while(it.hasNext()){ File temp= it.next(); System.out.println(temp.getName() + '\t' + temp.getAbsolutePath() + '\t' + temp.length()); } } public static void main(String[] args){ String filePath = args[0]; try { printDuplicates(getDuplicatedFile(filePath)); }catch (Exception e){} } static private class NotDirectoryExpectation extends Exception{}}
这个主要就是File类的使用了