尝试将 rtf 文件从 Jar 读取到 JEditorPane;在 Netbeans 中工作,而不是在 Jar 中工作
很抱歉,如果这是显而易见的,但我已经寻找了好几天,但似乎找不到答案。
情况是这样的。我正在尝试从 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);
}
非常感谢任何帮助!谢谢!
sorry if this is obvious, but I've been looking for days and I can't seem to find the answer.
Here is the situation. I am trying to read from an rtf file located within a jar file and then display it in a JEditorPane. This is working from the Netbeans environment, but not when executing the jar file in the dist folder. A version of this question has been asked before, but for my application I think I don't think the previous answers apply. (The reason is that JEditorPane.read requires a FileInputStream, and all of the solutions to other problems I have seen have involved non-File InputStreams. Here is the code in question:
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);
}
Any help is very appreciated! Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用从
getResource()
获取的URL
加载文档。如果它位于 Jar 中,则无法将其作为File
进行访问。像这样的东西:
Use the
URL
obtained fromgetResource()
to load the document. If it is in a Jar, it will not be accessible as aFile
.Something like this: