很抱歉,如果这是显而易见的,但我已经寻找了好几天,但似乎找不到答案。
情况是这样的。我正在尝试从 jar 文件中的 rtf 文件读取内容,然后将其显示在 JEditorPane 中。这在 Netbeans 环境中有效,但在执行 dist 文件夹中的 jar 文件时无效。以前曾问过这个问题的一个版本,但对于我的应用程序,我认为以前的答案不适用。 (原因是 JEditorPane.read 需要 FileInputStream,而我见过的所有其他问题的解决方案都涉及非文件 InputStream。以下是有问题的代码:
descPane = new JEditorPane("application/rtf", "");
try {
File test = new File(this.getClass().getResource("files/general.rtf").toURI());
descPane.read(new FileInputStream(test), null);
} catch (Exception ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
非常感谢任何帮助!谢谢!
最佳答案
使用从 getResource()
获取的 URL
加载文档。如果它位于 Jar 中,则无法将其作为文件
访问。
类似这样的事情:
URL urlToRtf = this.getClass().getResource("files/general.rtf");
descPane.setPage(urlToRtf);
关于java - 尝试将 rtf 文件从 Jar 读取到 JEditorPane;在 Netbeans 中工作,而不是在 Jar 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8766842/