猿问

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

我的目录中有大量图像。某些图像的问题在于它们具有名称相同但扩展名不同的重复项,例如image1.jpg,image1.jpeg,image1.png,它们都是相同的图像,名称相同但扩展名不同。如何使用Java查找和删除这些重复项?有很多用于查找重复项的工具,但我找不到针对此特定问题的任何工具或脚本。任何帮助将不胜感激。


暮色呼如
浏览 345回答 3
3回答

汪汪一只猫

将您的所有文件读入List某种形式的文件:List<File> filesInFolder = Files.walk(Paths.get("\\path\\to\\folder"))&nbsp; &nbsp; &nbsp; &nbsp; .filter(Files::isRegularFile)&nbsp; &nbsp; &nbsp; &nbsp; .map(Path::toFile)&nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());然后循环遍历它们,如果文件未以所需的扩展名结尾,则将其删除:filesInFolder.stream().filter((file) -> (!file.toString().endsWith(".jpg"))).forEach((file) -> {&nbsp; &nbsp; file.delete();});您可以根据自己的特定需求进行调整。

慕姐8265434

这是MCVE:本示例实现了Set通过仅提供包含图像的文件夹/目录的路径来自动删除重复图像的方法(只是一个不同的想法,以显示其他可用选项以及如何利用Java中的OO功能)import java.io.File;import java.util.HashSet;import java.util.Set;public class DuplicateRemover {&nbsp; &nbsp; // inner class to represent an image&nbsp; &nbsp; class Image{&nbsp; &nbsp; &nbsp; &nbsp; String path; // the absolute path of image file as a String&nbsp; &nbsp; &nbsp; &nbsp; // constructor&nbsp; &nbsp; &nbsp; &nbsp; public Image(String path) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.path = path;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public boolean equals(Object o) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(o instanceof Image){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // if both base names are equal -> delete the old one&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(getBaseName(this.path).equals(getBaseName(((Image)o).path))){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; File file = new File(this.path);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return file.delete();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public int hashCode() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 0; // in this case, only "equals()" method is considered for duplicate check&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/**&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * This method to get the Base name of the image from the path&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * @param fileName&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * @return&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */&nbsp; &nbsp; &nbsp; &nbsp; private String getBaseName(String fileName) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int index = fileName.lastIndexOf('.');&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (index == -1) { return fileName; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else { return fileName.substring(0, index); }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }&nbsp; &nbsp; Set<Image> images; // a set of image files&nbsp; &nbsp; //constructor&nbsp; &nbsp; public DuplicateRemover(){&nbsp; &nbsp; &nbsp; &nbsp; images = new HashSet<>();&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Get the all the images from the given folder&nbsp; &nbsp; &nbsp;* and loop through all files to add them to the images set&nbsp; &nbsp; &nbsp;* @param dirPath&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public void run(String dirPath){&nbsp; &nbsp; &nbsp; &nbsp; File dir = new File(dirPath);&nbsp; &nbsp; &nbsp; &nbsp; File[] listOfImages = dir.listFiles();&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; for (File f : listOfImages){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (f.isFile()) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; images.add(new Image(f.getAbsolutePath()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; //TEST&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; String dirPath = "C:\\Users\\Yahya Almardeny\\Desktop\\folder";&nbsp; &nbsp; &nbsp; &nbsp; /* dir contains: {image1.png, image1.jpeg, image1.jpg, image2.png}&nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; DuplicateRemover dr = new DuplicateRemover();&nbsp; &nbsp; &nbsp; &nbsp; // the images set will delete any duplicate image from the folder&nbsp; &nbsp; &nbsp; &nbsp; // according to the logic we provided in the "equals()" method&nbsp; &nbsp; &nbsp; &nbsp; dr.run(dirPath);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // print what images left in the folder&nbsp; &nbsp; &nbsp; &nbsp; for(Image image : dr.images) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(image.path);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //Note that you can use the set for further manipulation if you have in later&nbsp; &nbsp; }}结果C:\Users\Yahya Almardeny\Desktop\folder\image1.jpegC:\Users\Yahya Almardeny\Desktop\folder\image2.png
随时随地看视频慕课网APP

相关分类

Java
我要回答