Java 加载 PDF 文件文件不存在
Java Load PDF File File Doesnt Exist
我试图让我的 Java 应用程序在用户单击按钮时打开 PDF 文件。但是,我得到下面的堆栈跟踪,说明该文件不存在。基本上我希望能够在用户进行选择时加载此文件。
下面我将有堆栈跟踪,然后是代码和路径的屏幕截图。
堆栈跟踪:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: The file: \RFBase-TD_Communications\src\pdf\RFTDAnalyzerHelpFile.pdf doesn't exist.
at java.awt.Desktop.checkFileValidation(Unknown Source)
at java.awt.Desktop.open(Unknown Source)
at GUI.rfbgui.openPDF(rfbgui.java:787)
at GUI.rfbgui.access(rfbgui.java:773)
at GUI.rfbgui.actionPerformed(rfbgui.java:921)
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.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.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.access0(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.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)
代码:
private static void openPDF()
{
File pdfHelpFile = new File("/RFBase-TD_Communications/src/pdf/RFTDAnalyzerHelpFile.pdf");
try
{
Desktop.getDesktop().open(pdfHelpFile);
}catch(IOException ex)
{
ex.printStackTrace();
}
}
File myFile = new File(getClass().getResource("/files/test.pdf").toURI());
或
if (Desktop.isDesktopSupported()) {
try {
File myFile = new File("/path/to/file.pdf");
Desktop.getDesktop().open(myFile);
} catch (IOException ex) {
// no application registered for PDFs
}
我有一些关于如何处理这些情况的一般性建议。在开始学习编程时,文件是我感到非常沮丧的事情之一。
使用System.getProperty("user.dir"); 这会非常有用,尤其是当你不知道程序的运行方向时运行 来自,或者您有特定的文件结构。
在Java中,我一般推荐使用“\”而不是“/”。
运行 对您尝试加载的文件进行完整性检查。具体检查它是否为 null、.isFile() 等。您永远不知道会返回什么,因此最好在程序意外崩溃之前达到峰值。
以下是一些可能对您有所帮助的类似问题的链接;
How should I load files into my Java application?
Getting the Current Working Directory in Java
Getting the inputstream from a classpath resource (XML file)
我试图让我的 Java 应用程序在用户单击按钮时打开 PDF 文件。但是,我得到下面的堆栈跟踪,说明该文件不存在。基本上我希望能够在用户进行选择时加载此文件。
下面我将有堆栈跟踪,然后是代码和路径的屏幕截图。
堆栈跟踪:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: The file: \RFBase-TD_Communications\src\pdf\RFTDAnalyzerHelpFile.pdf doesn't exist.
at java.awt.Desktop.checkFileValidation(Unknown Source)
at java.awt.Desktop.open(Unknown Source)
at GUI.rfbgui.openPDF(rfbgui.java:787)
at GUI.rfbgui.access(rfbgui.java:773)
at GUI.rfbgui.actionPerformed(rfbgui.java:921)
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.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.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.access0(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.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)
代码:
private static void openPDF()
{
File pdfHelpFile = new File("/RFBase-TD_Communications/src/pdf/RFTDAnalyzerHelpFile.pdf");
try
{
Desktop.getDesktop().open(pdfHelpFile);
}catch(IOException ex)
{
ex.printStackTrace();
}
}
File myFile = new File(getClass().getResource("/files/test.pdf").toURI());
或
if (Desktop.isDesktopSupported()) {
try {
File myFile = new File("/path/to/file.pdf");
Desktop.getDesktop().open(myFile);
} catch (IOException ex) {
// no application registered for PDFs
}
我有一些关于如何处理这些情况的一般性建议。在开始学习编程时,文件是我感到非常沮丧的事情之一。
使用System.getProperty("user.dir"); 这会非常有用,尤其是当你不知道程序的运行方向时运行 来自,或者您有特定的文件结构。
在Java中,我一般推荐使用“\”而不是“/”。
运行 对您尝试加载的文件进行完整性检查。具体检查它是否为 null、.isFile() 等。您永远不知道会返回什么,因此最好在程序意外崩溃之前达到峰值。
以下是一些可能对您有所帮助的类似问题的链接;
How should I load files into my Java application?
Getting the Current Working Directory in Java
Getting the inputstream from a classpath resource (XML file)