文件迭代不处理资源文件夹



为什么我不能遍历本例中的文件:

private List<String> bilder;
private int bildNummer;

public void initialize(URL location, ResourceBundle bundle) {
// before maven: ladeBilder(new File("/img/settings"));
ladeBilder(new File(String.valueOf((SettingsController.class.getResource("/img/settings")))));
//imgView.setImage(uebergebeBilder());
}
private Image uebergebeBilder() {
if(bildNummer <= 13){
String bild = bilder.get(bildNummer);
bildNummer++;
return new Image("sample/img/settings/"+bild);
} else{
bildNummer = 0;
String bild = bilder.get(bildNummer);
bildNummer++;
return new Image("sample/img/settings/"+bild);
}
}
private void ladeBilder(final File pfad) {
if(bilder == null) {
bilder = new ArrayList<String>();
} else {
bilder.clear();
}
File[] files = pfad.listFiles((dir, name) -> {
if(name.equals(".DS_Store")){
return false;}
else if(name.equals("thumbs.db")){
return false;
}else{
return true;
}
});
for(File f : files) {
if(f.isDirectory()) {
ladeBilder(f);
} else {
bilder.add(f.getName());
}
}
}

在我添加maven框架之前,它起了作用。System.out.println(String.valueOf((SettingsController.class.getResource("/img/settings"((((;给了我以下路径:文件:/Users/***/***/Proggen/Passwort-Manager19/target/classes/img/settings图像被加载到该路径中。我插入了几个System.out.println(int(来确定在哪个位置一切正常,并且for(File f : files) {之后的System.out.print没有执行。(很抱歉,如果是JavaFX项目,我仍在阅读这些错误代码(

顺便说一句…如果我想做一个罐子,我还能用SettingsController.class.getResource("/img/settings")吗?

是的,我知道return new Image("sample/img/settings/"+bild);不是正确的道路,但这不是问题所在。

这是日志:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.control.MenuItem.fire(MenuItem.java:462)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1405)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$6(ContextMenuContent.java:1358)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:410)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 40 more
Caused by: javafx.fxml.LoadException: 
/Users/****/****/Proggen/Passwort-Manager19/target/classes/sample/SettingsView.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at sample.MainController.settings(MainController.java:222)
... 50 more
Caused by: java.lang.NullPointerException
at sample.SettingsController.ladeBilder(SettingsController.java:65)
at sample.SettingsController.initialize(SettingsController.java:30)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 53 more

显然,您更改了:

ladeBilder(new File("/img/settings"));

ladeBilder(new File(String.valueOf(
(SettingsController.class.getResource("/img/settings")))));

它停止工作了。问题是getResource没有返回文件名。它返回一个URL。然后使用String.valueOf(Object)URL转换为字符串,然后将该字符串视为文件名。

但它不是文件名。它是一个url字符串。它看起来像这样:

jar:file:/path/to/jar!/img/settings

因此,当您在该url字符串上new File(urlString)时,您将获得一个引用不存在文件的File对象。最终你会得到一个NPE(显然(。

如何解决此问题?

您要做的是迭代资源路径中给定树中的资源。简单地说,使用文件I/O库(FilePathFiles等(不能做到这一点,因为资源名称空间不是文件系统名称空间。

你需要使用另一种方法。有多种方法可以做到这一点;请参阅以下内容:

  • 如何浏览Java类资源
  • 如何在类路径上遍历目录树以查找资源文件

在我添加Maven框架之前,它就已经工作了。

这不是Maven的错。

更好的描述是,当您在开发环境中从文件系统访问资源时,它就起作用了。它崩溃了,因为您将资源包含在JAR文件中,并更改了代码以从那里访问它们。

无论您使用何种方法创建JAR文件,都会发生这种情况。

@StephenC:谢谢。这将是一个很好的解决方案,下次我真的需要这个。但经过一段时间的思考,我得出的结论是,我只是走了一条更容易的路。因为我知道所有的文件,所以我只使用以下方法(在开始时将bildNummer设置为1(:

private void Bilder(){
System.out.println("/img/settings/"+bildNummer+".jpg");
InputStream inputStream = SettingsController.class.getResourceAsStream("img/settings/"+bildNummer+".jpg");
imgView.setImage(new Image(inputStream));
if(bildNummer==14){
bildNummer=1;
}else{
bildNummer++;
}
}

相关内容

  • 没有找到相关文章