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

时间:2021-09-30 18:08:24

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? 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!

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

Thank you!

谢谢!

2 个解决方案

#1


3  

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.

Jar文件只是具有不同文件后缀的.zip文件,以及内容的命名约定。因此,使用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的内容听起来像复杂且容易出错的方法......

#2


1  

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.

如果你的应用程序有一个GUI,你可以访问一个网站/服务器,JWS可能是答案。 JWS应用程序可用的JNLP API。提供诸如PersistenceService之类的服务。这是一个小型演示。 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.

我们的想法是检查JWS持久性存储中的XML。如果不存在,请将其写入,否则使用缓存版本。如果更改,请将新版本写入商店。

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.

演示。在关机时写入商店,并在启动时读取。但没有理由不能通过菜单项,计时器等来调用它。

#1


3  

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.

Jar文件只是具有不同文件后缀的.zip文件,以及内容的命名约定。因此,使用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的内容听起来像复杂且容易出错的方法......

#2


1  

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.

如果你的应用程序有一个GUI,你可以访问一个网站/服务器,JWS可能是答案。 JWS应用程序可用的JNLP API。提供诸如PersistenceService之类的服务。这是一个小型演示。 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.

我们的想法是检查JWS持久性存储中的XML。如果不存在,请将其写入,否则使用缓存版本。如果更改,请将新版本写入商店。

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.

演示。在关机时写入商店,并在启动时读取。但没有理由不能通过菜单项,计时器等来调用它。