简体   繁体   English

在JAR中写入属性文件

[英]Writing to a properties file in a JAR

I'm create an application that is going to be run on Windows, Mac OX and Linux. 我正在创建一个将在Windows,Mac OX和Linux上运行的应用程序。 I have a properties file storing user settings which need to be read and changed on the fly. 我有一个属性文件,其中存储需要实时读取和更改的用户设置。

A JAR file is compressed and is not meant to be changed on the fly which means I should write to an external file. JAR文件已压缩,并不意味着可以随时更改,这意味着我应该写入外部文件。

I'm using : 我正在使用 :

new FileInputStream("database")

new FileOutputStream("database")

How do I create a URL which is going to be consistent throughout all three operating systems. 如何创建将在所有三个操作系统中保持一致的URL。 The JAR is run as an application on the desktop and I would like the file to be stored somewhere discrete. JAR在桌面上作为应用程序运行,我希望将文件存储在离散的位置。

I've tried reading from a local file in the same package as this class : 我试图从与此类相同的包中的本地文件中读取:

this.getClass().getResourceAsStream("database")

This works however I can't seem to create an output stream to write to the same file but this would be breaking the rule of changing a JAR file on the fly. 这行得通,但是我似乎无法创建输出流来写入同一文件,但这将违反即时更改JAR文件的规则。

There are plenty of good reasons for which you should not do this, off the top of my head: 有很多充分的理由使您不应该这样做,这是我的首要考虑:

  • GetResourceAsStream does not necessarily get the file from the JAR itself. GetResourceAsStream不一定从JAR本身获取文件。 You coincidentally got it from there because the Jar was the first or the only element in the class path. 您偶然从那里得到了它,因为Jar是类路径中的第一个或唯一元素。
  • Writing a file in your own Jar could break the JAR signature if you are going to sign it. 如果要签名,则在自己的Jar中写入文件可能会破坏JAR签名。
  • The database could need to be backed up; 数据库可能需要备份; in this case you may want to back it up separate from the code (the code could be upgraded when restoring the database). 在这种情况下,您可能希望将其与代码分开备份(还原数据库时可以升级代码)。

Hope this helps. 希望这可以帮助。

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

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