gpt4 book ai didi

java - 直接从 src 打开 docx 文件生成 "File not found"异常

转载 作者:行者123 更新时间:2023-12-02 04:01:58 25 4
gpt4 key购买 nike

编辑:

我正在尝试打开存储在项目文件 SRC 中的文件。我收到“找不到文件”异常,我真的不明白为什么。该文件是一个 docx,我已将其放入 eclipse 中的项目的 src 文件夹中。

这是我尝试打开文件的代码:

JButton documentationButton = new JButton("Helpfile");

documentationButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent ae){

Desktop desktop = null;
if(Desktop.isDesktopSupported()){
desktop = Desktop.getDesktop();
}

try {
ClassLoader classLoader = getClass().getClassLoader();
File fi = new File(classLoader.getResource("ReturnBuilderDocumentation.docx").getFile());

String string = "file:/src/ReturnBuilderDocumentation.docx";

desktop.open(new File(URI.create(string)));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



}

});

这是生成的异常:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: The file: \src\ReturnBuilderDocumentation.docx doesn't exist.
at java.awt.Desktop.checkFileValidation(Unknown Source)
at java.awt.Desktop.open(Unknown Source)
at nachamultifive.GUIs.MainMenu$3.actionPerformed(MainMenu.java:172)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(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$500(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$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.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$JavaSecurityAccessImpl.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)

这个文件显然在我的项目 src 中,所以我不知道为什么会生成错误。任何帮助是极大的赞赏。

更新:

我还尝试使用反斜杠,但生成了另一个错误。

代码:

String string = "\\src\\ReturnBuilderDocumentation.docx";

错误:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Illegal character in opaque part at index 5: file:\src\ReturnBuilderDocumentation.docx
at java.net.URI.create(Unknown Source)
at nachamultifive.GUIs.MainMenu$3.actionPerformed(MainMenu.java:172)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(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$500(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$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.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$JavaSecurityAccessImpl.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)
Caused by: java.net.URISyntaxException: Illegal character in opaque part at index 5: file:\src\ReturnBuilderDocumentation.docx
at java.net.URI$Parser.fail(Unknown Source)
at java.net.URI$Parser.checkChars(Unknown Source)
at java.net.URI$Parser.parse(Unknown Source)
at java.net.URI.<init>(Unknown Source)
... 38 more

最佳答案

此代码存在问题,因为它在给定路径中找不到该文件

String string = "file:/src/ReturnBuilderDocumentation.docx";
desktop.open(new File(URI.create(string)));

原因是代码编译后,src文件夹并不真正存在。

要获取编译后的文件资源,必须使用

getClass().getClassLoader().getResource()

你在这里做得正确

ClassLoader classLoader = getClass().getClassLoader();
File fi = new File(classLoader.getResource("ReturnBuilderDocumentation.docx").getFile());

因此要打开文件,您只需执行以下操作

desktop.open(fi);

而不是

desktop.open(new File(URI.create(string)));

关于java - 直接从 src 打开 docx 文件生成 "File not found"异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34793054/

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