使用 Java 打开并编辑 .jar 中的文件?
您将如何打开 .jar 中的 .xml 文件并对其进行编辑?
我知道你可以做...
InputStream myStream = this.getClass().getResourceAsStream("xmlData.xml");
但是你如何打开 xmlData.xml,编辑文件并将其保存在 .jar 中?我发现了解这一点很有用,并且不想编辑 .jar 之外的文件...并且应用程序需要始终保持运行!
谢谢!
How would you go about opening an .xml file that is within a .jar and edit it?
I know that you can do...
InputStream myStream = this.getClass().getResourceAsStream("xmlData.xml");
But how would you open the xmlData.xml, edit the file, and save it in the .jar? I would find this useful to know and don't want to edit a file outside of the .jar... and the application needs to stay running the entire time!
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Jar 文件只是具有不同文件后缀和内容命名约定的 .zip 文件。因此,请使用 java.util.zip 下的类来读取和/或写入内容。
修改内容不能保证(甚至可能)影响正在运行的系统,因为类加载器可能会缓存它认为合适的内容。
因此,最好更多地了解您实际上想通过此实现什么目标。即时修改 jar 的内容听起来像是复杂且容易出错的方法......
Jar files are just .zip files with different file suffix, and naming convention for contents. So use classes from under
java.util.zip
to read and/or write contents.Modifying contents is not guaranteed (or even likely) to effect running system, as class loader may cache contents as it sees fit.
So it might be good to know more about what you are actually trying to achieve with this. Modifying contents of a jar on-the-fly sounds like complicated and error-prone approach...
如果你应用。拥有 GUI 并且您可以访问网站/服务器,JWS 可能就是答案。可用于 JWS 应用程序的 JNLP API。提供 PersistenceService 等服务。这是一个小型演示。 PersistenceService 的。
这个想法是检查 JWS 持久性存储中的 XML。如果不存在,则将其写入此处,否则使用缓存版本。如果发生变化,请将新版本写入商店。
演示。在关闭时写入存储,并在启动时读取。但没有理由不能通过菜单项、计时器等来调用它。
If you app. has a GUI and you have access to a web site/server, JWS might be the answer. The JNLP API that is available to JWS apps. provides services such as the PersistenceService. Here is a small demo. of the PersistenceService.
The idea would be to check for the XML in the JWS persistence store. If it is not there, write it there, otherwise use the cached version. If it changes, write a new version to the store.
The demo. writes to the store at shut-down, and reads at start-up. But there is no reason it could not be called by a menu item, timer etc.