尝试将 rtf 文件从 Jar 读取到 JEditorPane;在 Netbeans 中工作,而不是在 Jar 中工作

发布于 2024-12-25 18:50:51 字数 633 浏览 2 评论 0原文

很抱歉,如果这是显而易见的,但我已经寻找了好几天,但似乎找不到答案。

情况是这样的。我正在尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

善良天后 2025-01-01 18:50:51

使用从 getResource() 获取的 URL 加载文档。如果它位于 Jar 中,则无法将其作为 File 进行访问。

像这样的东西:

URL urlToRtf = this.getClass().getResource("files/general.rtf");
descPane.setPage(urlToRtf);

Use the URL obtained from getResource() to load the document. If it is in a Jar, it will not be accessible as a File.

Something like this:

URL urlToRtf = this.getClass().getResource("files/general.rtf");
descPane.setPage(urlToRtf);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文