DirectoryStream <path>以偶然的方式列出文件</path>

时间:2012-09-13 18:56:16

标签: java image-processing path directory

我必须从文件夹中检索一组JPG文件,以便我可以用它创建一个电影。问题是文件以偶然的方式列出,因此以随意的方式处理。这是一个截图:
enter image description here

我需要它们以顺序的方式作为img0,img1 ..... imgn
我该怎么做?
**我开发了一个自定义排序方案,但我得到了一个例外。编码在这里给出:&#34;

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)

2 个答案:

答案 0 :(得分: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。引自its reference

  

虽然DirectoryStream延伸Iterable,但它不是。{   通用Iterable因为它只支持一个Iterator;   调用迭代器方法来获取第二个或后续的迭代器   抛出IllegalStateException

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

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

请注意,有关IllegalStateException的此问题与您提出的原始问题有很大不同。所以在一个单独的问题中会更好。请记住,SO是not a forum。所以请坚持每个问题一个问题。

答案 1 :(得分:1)

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