代码之家  ›  专栏  ›  技术社区  ›  Asmat Ali

如何删除同名但扩展名不同的重复文件?

  •  1
  • Asmat Ali  · 技术社区  · 7 年前

    3 回复  |  直到 7 年前
        1
  •  1
  •   Leviand    7 年前

        public class FileUtil {
        String fileName;
        File file;
        boolean delete = true;
    
    
        public FileUtil(String fileName, File file) {
            super();
            this.fileName = fileName.substring(0, fileName.indexOf("."));
            this.file = file;
        }
    
        public String getFileName() {
            return fileName;
        }
        public void setFileName(String fileName) {
            this.fileName = fileName;
        }
        public File getFile() {
            return file;
        }
        public void setFile(File file) {
            this.file = file;
        }
        public boolean isDelete() {
            return delete;
        }
        public void setDelete(boolean delete) {
            this.delete = delete;
        }
    
        @Override
        public String toString() {
            return "FileUtil [fileName=" + fileName + ", file=" + file + ", delete=" + delete + "]";
        }
    
    }
    

    try (Stream<Path> paths = Files.walk(Paths.get("c:/yourPath/"))) {
            List<FileUtil> listUtil = new ArrayList<FileUtil>();
    
            paths
                .filter(Files::isRegularFile)
                .map(filePath -> filePath.toFile())
                .collect(Collectors.toList())
                .forEach(file -> listUtil.add(new FileUtil(file.getName(), file)));
    
            Map<String, List<FileUtil>> collect = listUtil.stream()
                    .collect(Collectors.groupingBy(FileUtil::getFileName));
    
            for(String key : collect.keySet() ) {
                List<FileUtil> list = collect.get(key);
                if(list.size() > 1) {
                    list.stream().findFirst().ifPresent(f -> f.setDelete(false));
    
                    list.stream()
                        .filter(fileUtil -> fileUtil.isDelete())
                        .forEach(fileUtil -> fileUtil.getFile().delete());
                }
            }
    
    
        } catch (IOException e) {
            e.printStackTrace();
        } 
    

        2
  •  1
  •   achAmháin    7 年前

    List

    List<File> filesInFolder = Files.walk(Paths.get("\\path\\to\\folder"))
            .filter(Files::isRegularFile)
            .map(Path::toFile)
            .collect(Collectors.toList());
    

    filesInFolder.stream().filter((file) -> (!file.toString().endsWith(".jpg"))).forEach((file) -> {
        file.delete();
    });
    

        3
  •  1
  •   Yahya    7 年前

    MCVE

    Set

    import java.io.File;
    import java.util.HashSet;
    import java.util.Set;
    
    public class DuplicateRemover {
    
        // inner class to represent an image
        class Image{
            String path; // the absolute path of image file as a String
    
            // constructor
            public Image(String path) {
                this.path = path;
            }       
    
            @Override
            public boolean equals(Object o) {
                if(o instanceof Image){
                    // if both base names are equal -> delete the old one
                    if(getBaseName(this.path).equals(getBaseName(((Image)o).path))){
                        File file = new File(this.path);
                        return file.delete();
                    }
                }
                return false;
            }
    
            @Override
            public int hashCode() {
                return 0; // in this case, only "equals()" method is considered for duplicate check
             } 
    
             /**
              * This method to get the Base name of the image from the path
              * @param fileName
              * @return
              */
            private String getBaseName(String fileName) {
                int index = fileName.lastIndexOf('.'); 
                if (index == -1) { return fileName; } 
                else { return fileName.substring(0, index); }
             }
        }
    
    
        Set<Image> images; // a set of image files
    
        //constructor
        public DuplicateRemover(){
            images = new HashSet<>();
        } 
    
        /**
         * Get the all the images from the given folder
         * and loop through all files to add them to the images set
         * @param dirPath
         */
        public void run(String dirPath){
            File dir = new File(dirPath);
            File[] listOfImages = dir.listFiles(); 
            for (File f : listOfImages){
                if (f.isFile()) { 
                    images.add(new Image(f.getAbsolutePath()));
                }
            }
        }
    
    
        //TEST
        public static void main(String[] args) {
            String dirPath = "C:\\Users\\Yahya Almardeny\\Desktop\\folder";
            /* dir contains: {image1.png, image1.jpeg, image1.jpg, image2.png}       */
            DuplicateRemover dr = new DuplicateRemover();
            // the images set will delete any duplicate image from the folder
            // according to the logic we provided in the "equals()" method
            dr.run(dirPath); 
    
            // print what images left in the folder
            for(Image image : dr.images) {
                System.out.println(image.path);
            }
    
            //Note that you can use the set for further manipulation if you have in later
        }
    
    }
    

    C:\Users\Yahya Almardeny\Desktop\folder\image1.jpeg
    C:\Users\Yahya Almardeny\Desktop\folder\image2.png