gpt4 book ai didi

java - DocumentBuilder 从绝对路径解析文件

转载 作者:行者123 更新时间:2023-12-01 23:55:35 25 4
gpt4 key购买 nike

我有一个带有 JFileChooser 的基本 Java-UI。

我尝试使用以下代码解析所选文件:

Document document = builder.parse(getClass().getResourceAsStream(<SELECTED_PATH>));

遗憾的是我得到了这个输出:

java.lang.IllegalArgumentException: InputStream cannot be null
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:120)
at wordpress2myblog.WordpressBlog.importWP(WordpressBlog.java:59)
at wordpress2myblog.WPImportUI.jButton1MouseClicked(WPImportUI.java:165)
at wordpress2myblog.WPImportUI.access$000(WPImportUI.java:16)
at wordpress2myblog.WPImportUI$1.mouseClicked(WPImportUI.java:51)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:253)
at java.awt.Component.processMouseEvent(Component.java:6385)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
at java.awt.Component.processEvent(Component.java:6147)
at java.awt.Container.processEvent(Container.java:2083)
at java.awt.Component.dispatchEventImpl(Component.java:4744)
at java.awt.Container.dispatchEventImpl(Container.java:2141)
at java.awt.Component.dispatchEvent(Component.java:4572)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4619)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4289)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4210)
at java.awt.Container.dispatchEventImpl(Container.java:2127)
at java.awt.Window.dispatchEventImpl(Window.java:2489)
at java.awt.Component.dispatchEvent(Component.java:4572)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:710)
at java.awt.EventQueue.access$400(EventQueue.java:82)
at java.awt.EventQueue$2.run(EventQueue.java:669)
at java.awt.EventQueue$2.run(EventQueue.java:667)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$3.run(EventQueue.java:683)
at java.awt.EventQueue$3.run(EventQueue.java:681)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:680)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:205)
at java.awt.Dialog$1.run(Dialog.java:1049)
at java.awt.Dialog$3.run(Dialog.java:1101)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.Dialog.show(Dialog.java:1099)
at java.awt.Component.show(Component.java:1591)
at java.awt.Component.setVisible(Component.java:1543)
at java.awt.Window.setVisible(Window.java:843)
at java.awt.Dialog.setVisible(Dialog.java:984)
at wordpress2myblog.WPImportUI$3.run(WPImportUI.java:222)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:708)
at java.awt.EventQueue.access$400(EventQueue.java:82)
at java.awt.EventQueue$2.run(EventQueue.java:669)
at java.awt.EventQueue$2.run(EventQueue.java:667)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:678)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

如果我尝试使用硬编码的相对路径解析文件,则代码可以正常工作。但随着从文件选择器获取的路径,这些错误会终止我的程序。

最佳答案

使用 JFileChooser 中的 getSelectedFile() 方法并使用返回的 File 创建一个 InputStream。

JFileChooser chooser = new JFileChooser();
File file = chooser.getSelectedFile();
InputStream inputStream = new FileInputStream(file);
Document document = builder.parse(inputStream);

关于java - DocumentBuilder 从绝对路径解析文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15672013/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com