我必须从文件夹中检索一组 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)