比较 Java 中的文件列表

huangapple go评论57阅读模式
英文:

Compare a List of files in java

问题

我正在尝试实现一个JavaFx应用程序,其中将比较mp4文件的文件名和png文件的文件名,如果它们相等或者一个包含另一个,目前会输出sysout。

到目前为止,我正在使用两个循环遍历文件列表,并且正在检索这两个文件,但存在一个问题:我有比mp4文件更多的png文件,这就是为什么我会得到ArrayOutOfBounds异常的原因。我确实找不到这个问题的解决方法。问题出在最后的if语句中。

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");
    }
}
英文:

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(&quot;C:\\Users\\Hasan\\OneDrive\\Desktop\\Smart-Moniesta\\Smart-Moniesta-Raspberry-Pi\\src\\ressources\\videos&quot;);
    File[] listOfFiles = videoDirectory.listFiles();
    File imgDirectory = new File(&quot;C:\\Users\\Hasan\\OneDrive\\Desktop\\Smart-Moniesta\\Smart-Moniesta-Raspberry-Pi\\src\\ressources\\images&quot;);
    File[] imglistOfFiles = imgDirectory.listFiles();

    int i = 0;
    int j = 0;

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

答案1

得分: 1

你需要将第二个for循环嵌套在第一个循环内部。你最后的if条件也应该在内部的for循环中。

你还需要在你的for循环中添加另一个条件,检查第二个文件名是否包含第一个文件名。

英文:

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.

huangapple
  • 本文由 发表于 2020年9月15日 23:12:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63904835.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定