-1

我必须从文件夹中检索一组 JPG 文件,以便我可以用它创建电影。问题是文件以随意的方式列出,因此以随意的方式处理。这是一个屏幕截图:
在此处输入图像描述

我需要它们按顺序排列为 img0,img1 .....imgn
我该怎么做?
**我开发了一个自定义排序方案,但出现异常。编码在这里给出:“

try{
                int totalImages = 0;
                int requiredImage = 0;
                jpegFiles = Files.newDirectoryStream(Paths.get(pathToPass).getParent(), "*.JPG");
                Iterator it = jpegFiles.iterator();
                Iterator it2 = jpegFiles.iterator();
                Iterator it3 = jpegFiles.iterator();
                while(it.hasNext()){
                    it.next();
                    totalImages++;
                }
                System.out.println(totalImages);
                sortedJpegFiles = new String[totalImages];
                while(it2.hasNext()){
                    Path jpegFile = (Path)it2.next();
                    String name = jpegFile.getFileName().toString();
                    int index = name.indexOf(Integer.toString(requiredImage));
                    if(index!=-1){
                        sortedJpegFiles[requiredImage] = jpegFile.toString();
                    }
                    requiredImage++;
                }
                for(String print : sortedJpegFiles){
                    System.out.println(print);
                }

            }catch(IOException e){
                e.printStackTrace();  
            }  

例外

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Iterator already obtained
    at sun.nio.fs.WindowsDirectoryStream.iterator(Unknown Source)
    at ScreenshotDemo.SCapGUI$VideoCreator.run(SCapGUI.java:186)
    at ScreenshotDemo.SCapGUI$1.windowClosing(SCapGUI.java:30)
    at java.awt.Window.processWindowEvent(Unknown Source)
    at javax.swing.JFrame.processWindowEvent(Unknown Source)
    at java.awt.Window.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
4

2 回答 2

3

自定义比较器

您必须提取数字部分,可能使用一些正则表达式,然后使用它进行排序。自定义Comparator可能会为您封装自定义数值比较。

import java.util.*;
import java.util.regex.*;

// Sort strings by last integer number contained in string.
class CompareByLastNumber implements Comparator<String> {
    private static Pattern re = Pattern.compile("[0-9]+");
    public boolean equals(Object obj) {
        return obj != null && obj.getClass().equals(getClass());
    }
    private int num(String s) {
        int res = -1;
        Matcher m = re.matcher(s);
        while (m.find())
            res = Integer.parseInt(m.group());
        return res;
    }
    public int compare(String a, String b) {
        return num(a) - num(b);
    }
    public static void main(String[] args) {
        Arrays.sort(args, new CompareByLastNumber());
        for (String s: args)
            System.out.println(s);
    }
}

避免IllegalStateException

DirectoryStream参考您更新的问题:您遇到的异常是因为您尝试多次迭代。引用其参考

虽然DirectoryStreamextends Iterable,它不是通用的Iterable,因为它只支持一个Iterator; 调用迭代器方法以获得第二个或后续迭代器 throws IllegalStateException

所以你看到的行为是可以预料的。您应该将所有文件收集到一个列表中。这些方面的东西:

List<Path> jpegFilesList = new ArrayList<>();
while (Path p: jpegFiles)
  jpegFilesList.add(p);
sortedJpegFiles = new String[jpegFilesList.size()];
for(Path jpegFile: jpegFilesList) {
  …
}

请注意,这个关于 IllegalStateException 的问题与您提出的原始问题截然不同。所以最好单独提出一个问题。请记住,SO不是论坛。所以请坚持每个问题一个问题。

于 2012-09-13T18:59:39.827 回答
1

这些文件是按字典顺序排列的,这对人类来说可能看起来不自然,但对于计算机来说却是完全合乎逻辑的。
如果您不想自己做,有几种算法可以根据“人类逻辑”进行排序,一个例子是The Alphanum Algorithm

于 2012-09-13T18:59:59.507 回答