基于this solution我正在访问pdf文件。代码如下:
editor.getMntmNewMenuItem().addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
try {
File pdfFile = new File("Ressources\\test.pdf");
if (pdfFile.exists()) {
if (Desktop.isDesktopSupported()) {
System.out.println(pdfFile.getCanonicalPath());
Desktop.getDesktop().open(pdfFile);
} else {
throw new Exception("Desktop wird nicht unterstützt!");
}
}
else {
throw new Exception("Datei ist nichtdd vorhanden! ");
}
} catch (Exception ex) {
PrintWriter pw = null;
try {
pw = new PrintWriter(new File("stacktrace.txt"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ex.printStackTrace(pw);
pw.append("\n\nUSER DIR: + " +System.getProperty("user.dir"));
pw.close();
JOptionPane.showMessageDialog(editor.getContentPane(), ex.getMessage(), "Fehler",
JOptionPane.ERROR_MESSAGE);
}
}
});
文件结构如下:
这是完整的stackTrace:
java.io.IOException:无法打开文件:/ E://Ressources/test.pdf。错误消息:系统找不到指定的文件。
at sun.awt.windows.WDesktopPeer.ShellExecute(Unknown Source)
at sun.awt.windows.WDesktopPeer.open(Unknown Source)
at java.awt.Desktop.open(Unknown Source)
at iscms.ISCMS$2$20.actionPerformed(ISCMS.java:877)
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.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)
USER DIR: = E:\
这在我的电脑和日食中完美运行,但在我的USB棒上它不起作用。我出于某种原因得到了IOException。我错过了什么?
答案 0 :(得分:0)
下面的解决方案确实有效。我不再使用相对路径,而是使用System.get.property("user.dir")
。我不知道这是不是很干净,但为什么抱怨这个有效呢?它没有回答这个问题,但它确实解决了我的问题。
editor.getMntmNewMenuItem().addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
try {
String userDir=System.getProperty("user.dir");
File pdfFile = new File(userDir+"\\Ressources\\test.pdf");
if (pdfFile.exists()) {
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().open(pdfFile);
} else {
throw new Exception("Desktop wird nicht unterstützt!");
}
}
else {
throw new Exception("Datei ist nicht vorhanden! ");
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(editor.getContentPane(), ex.getMessage(), "Fehler",
JOptionPane.ERROR_MESSAGE);
}
}
});