zoukankan      html  css  js  c++  java
  • 发现同一文件夹下的重复文件

    import java.io.*;
    import java.util.*;
    
    public class FindDuplicatedFile {
    
            //fileList is the list of files in this directory
        static private List<File> fileList = 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<fileArray.length; i++){
                if(fileArray[i].isFile())
                    fileList.add(fileArray[i]);
    
                if(fileArray[i].isDirectory())
                    getFileList(fileArray[i].getAbsolutePath());
            }
        }
    
    
    
            //to search the directory to find duplicate files
        public static List<File> getDuplicatedFile(String filePath)
                         throws NotDirectoryExpectation{
            getFileList(filePath);
            List<File > 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<File> list){
    
            Iterator<File> 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() + '	' +
                        temp.getAbsolutePath() + '	' + 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类的使用了

    你若笃定,世界便不浮躁。
  • 相关阅读:
    机会的三种境界
    常用“快”捷键
    心路历程
    中兴笔试及答案
    浅谈oracle中row_number() over()分析函数用法
    IE的F12开发人员工具不显示问题
    1002.A + B Problem II --大数问题
    6470.count --快速矩阵幂
    4151.电影--贪心
    3070.斐波拉契数列--快速幂
  • 原文地址:https://www.cnblogs.com/zhangyue123/p/9321420.html
Copyright © 2011-2022 走看看