第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何刪除名稱相同但擴展名不同的重復文件?

如何刪除名稱相同但擴展名不同的重復文件?

暮色呼如 2021-04-30 14:15:28
我的目錄中有大量圖像。某些圖像的問題在于它們具有名稱相同但擴展名不同的重復項,例如image1.jpg,image1.jpeg,image1.png,它們都是相同的圖像,名稱相同但擴展名不同。如何使用Java查找和刪除這些重復項?有很多用于查找重復項的工具,但我找不到針對此特定問題的任何工具或腳本。任何幫助將不勝感激。
查看完整描述

3 回答

?
汪汪一只貓

TA貢獻1898條經驗 獲得超8個贊

將您的所有文件讀入List某種形式的文件:


List<File> filesInFolder = Files.walk(Paths.get("\\path\\to\\folder"))

        .filter(Files::isRegularFile)

        .map(Path::toFile)

        .collect(Collectors.toList());

然后循環(huán)遍歷它們,如果文件未以所需的擴展名結尾,則將其刪除:


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

    file.delete();

});

您可以根據(jù)自己的特定需求進行調整。


查看完整回答
反對 回復 2021-05-12
?
慕姐8265434

TA貢獻1813條經驗 獲得超2個贊

這是MCVE:

本示例實現(xiàn)了Set通過僅提供包含圖像的文件夾/目錄的路徑來自動刪除重復圖像的方法(只是一個不同的想法,以顯示其他可用選項以及如何利用Java中的OO功能)


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


查看完整回答
反對 回復 2021-05-12
  • 3 回答
  • 0 關注
  • 378 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號