简体   繁体   中英

Compare a List of files in java

I am trying to implement a JavaFx Application where the filename of an mp4 and the filename of an png are compared, and if they are equal or one contains the other, right now there is a sysout.

So far I am using two loops to go through the lists of files and I am retrieving both files, but there is one problem: I have more pngs than mp4s, and that is why I am getting an ArrayOutOfBounds exception. I could not really find a workaround for this. The problem lies in the final if statement.

public void videoLoop() {
    File videoDirectory = new File("C:\\Users\\Hasan\\OneDrive\\Desktop\\Smart-Moniesta\\Smart-Moniesta-Raspberry-Pi\\src\\ressources\\videos");
    File[] listOfFiles = videoDirectory.listFiles();
    File imgDirectory = new File("C:\\Users\\Hasan\\OneDrive\\Desktop\\Smart-Moniesta\\Smart-Moniesta-Raspberry-Pi\\src\\ressources\\images");
    File[] imglistOfFiles = imgDirectory.listFiles();

    int i = 0;
    int j = 0;

    for (i = 0; i <= listOfFiles.length - 1; i++) {
        if (listOfFiles[i].isFile()) {
            System.out.println("File aus der i Schleife " + listOfFiles[i].getName());
        }
    }
    for (j = 0; j <= imglistOfFiles.length - 1; j++) {
        if (imglistOfFiles[j].isFile()) {
            System.out.println("File Bilder aus der j Schleife: " + imglistOfFiles[j].getName());
        }
    }
    if (listOfFiles[i].getName().contains(imglistOfFiles[j].getName())) {
        System.out.println("Vergleich klappt" + listOfFiles[i].getName());
    } else {
        System.out.println("KLappt nicht");
    }
}

You need the second for loop to be nested inside the first loop. and your last if condition should be inside the inner for loop.

You also need another condition in your for loop where you check if the second file name contains the first one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM