简体   繁体   English

使用Java在.jar中打开并编辑文件?

[英]Open and edit file in .jar with Java?

How would you go about opening an .xml file that is within a .jar and edit it? 您将如何打开.jar中的.xml文件并进行编辑?

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? 但是,如何打开xmlData.xml,编辑文件并将其保存在.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! 我会发现知道并且不想编辑.jar之外的文件很有用... 并且应用程序需要在整个时间内保持运行!

Thank you! 谢谢!

Jar files are just .zip files with different file suffix, and naming convention for contents. Jar文件只是具有不同文件后缀的.zip文件,以及内容的命名约定。 So use classes from under java.util.zip to read and/or write contents. 因此,使用java.util.zip下的类来读取和/或写入内容。

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... 即时修改jar的内容听起来像复杂且容易出错的方法......

If you app. 如果你的应用程序 has a GUI and you have access to a web site/server, JWS might be the answer. 有一个GUI,你可以访问一个网站/服务器,JWS可能是答案。 The JNLP API that is available to JWS apps. JWS应用程序可用的JNLP API。 provides services such as the PersistenceService. 提供诸如PersistenceService之类的服务。 Here is a small demo. 这是一个小型演示。 of the PersistenceService . PersistenceService

The idea would be to check for the XML in the JWS persistence store. 我们的想法是检查JWS持久性存储中的XML。 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. 但没有理由不能通过菜单项,计时器等来调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM