Java-如何发现用户更改了配置文件?

发布于 2024-08-24 09:14:57 字数 467 浏览 4 评论 0原文

我正在开发一个Java 桌面应用程序。此应用程序需要配置才能启动。为此,我想为应用程序提供一个 defaultConfig.propertiesdefaultConfig.xml 文件,以便如果用户未选择任何配置,则应用程序将以defaultConfig 文件的帮助。

但我担心如果用户不小心编辑了 defaultConfig 文件,我的应用程序就会崩溃。那么有没有什么机制可以让我在应用程序启动之前检查 config 文件是否已更改。

其他应用程序(市场上的)如何处理其应用程序依赖于配置文件的这种情况?


如果用户无意或故意编辑了配置文件,那么该应用程序将来将无法运行,除非他重新安装该应用程序。

I am developing a Java Desktop Application. This app needs a configuration to be started. For this, I want to supply a defaultConfig.properties or defaultConfig.xml file with the application so that If user doesn't select any configuration, then the application will start with the help of defaultConfig file.

But I am afraid of my application crash if the user accidentally edit the defaultConfig file. So Is there any mechanism through which I can check before the start of the application that whether the config file has changed or not.

How other applications (out in the market) deal with this type of situation in which their application depends on a configuration file?

If the user edited the config file accidentally or intentionally, then the application won't run in future unless he re-installs the application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

┾廆蒐ゝ 2024-08-31 09:14:57

我同意 大卫 因为使用 MD5 哈希是完成您想要的任务的一种很好且简单的方法。

基本上,您将使用 JDK(或其他地方)提供的 MD5 哈希代码根据 Config.xml 中的默认数据生成哈希代码,并将该哈希代码保存到文件中(或将其硬编码到函数中)进行检查)。然后,每次应用程序启动时,都会加载您保存到文件中的哈希代码,然后加载 Config.xml 文件并再次从中生成哈希代码,将保存的哈希代码与从加载的配置生成的哈希代码进行比较文件中,如果相同则数据没有改变,如果不同则数据被修改。

然而,正如其他人建议的那样,如果用户不应编辑该文件,那么您应该考虑以用户无法轻松编辑的方式存储配置。我能想到的最简单的方法是将用于编写 Config.xml 文件的输出流包装在 GZIP 输出流中。这不仅会导致用户编辑配置文件变得困难,而且还会导致 Config.xml 文件占用更少的空间。

I agree with David in that using a MD5 hash is a good and simple way to accomplish what you want.

Basically you would use the MD5 hashing code provided by the JDK (or somewhere else) to generate a hash-code based on the default data in Config.xml, and save that hash-code to a file (or hardcode it into the function that does the checking). Then each time your application starts load the hash-code that you saved to the file, and then load the Config.xml file and again generate a hash-code from it, compare the saved hash-code to the one generated from the loaded config file, if they are the same then the data has not changed, if they are different, then the data has been modified.

However as others are suggesting if the file should not be editable by the user then you should consider storing the configuration in a manner that the user can not easily edit. The easiest thing I can think of would be to wrap the Output Stream that you are using to write the Config.xml file in a GZIP Output Stream. Not only will this make it difficult for the user to edit the configuration file, but it will also cause the Config.xml file to take up less space.

新雨望断虹 2024-08-31 09:14:57

我完全不确定这是一个好方法,但如果您想继续这样做,您可以计算配置文件的哈希值(例如 md5),并在每次应用程序启动时重新计算和比较。
想想看,如果禁止用户编辑文件为什么要公开它呢?例如,将其放在 jar 文件中,远离用户的眼睛。

I am not at all sure that this is a good approach but if you want to go ahead with this you can compute a hash of the configuration file (say md5) and recompute and compare every time the app starts.
Come to think of it, if the user is forbidden to edit a file why expose it? Stick it in a jar file for example, far away from the user's eyes.

悸初 2024-08-31 09:14:57

如果默认配置不应该被编辑,也许您一开始就不想将其存储在文件中?你能不能直接在代码中存储配置的默认值吗?

If the default configuration is not supposed to be edited, perhaps you don't really want to store it in a file in the first place? Could you not store the default values of the configuration in the code directly?

無心 2024-08-31 09:14:57
  1. 删除该文件的写入权限。这样,用户在尝试更改文件之前会收到警告。
  2. 添加哈希值或校验和并在加载文件之前对其进行验证
  3. 为了提高安全性,您可以使用加密签名替换简单哈希值。
  1. Remove write permissions for the file. This way the user gets a warning before trying to change the file.
  2. Add a hash or checksum and verify this before loading file
  3. For added security, you can replace the simple hash with a cryptographic signature.
各自安好 2024-08-31 09:14:57

到目前为止,我在网上发现似乎有不同的代码方法。似乎没有一个是百分百修复的,例如:

DirectoryWatcher 实现
AbstractResourceWatcher 来监视
指定目录。

代码在这里找到 twit88.com 开发-a-java-文件观察器

遇到的一个问题是如果我复制
来自远程网络的大文件
本地目录的源是
受监控,该文件仍会显示
在目录列表中,但是
在网络复制完成之前。
如果我尝试做几乎任何非
此时对文件来说微不足道
就像将其移动到另一个目录或
打开它进行写入,例外情况
被抛出,因为实际上该文件是
尚未完全存在和操作系统
仍然有写锁。

在同一网站上找到,位于下面。

程序如何工作 它接受一个 ResourceListener 类,即 FileListener。如果在程序中检测到更改,则会引发 onAdd、onChange 或 onDelete 事件并将文件传递给。

将继续寻找更多解决方案。

From I have found online so far there seems to be different approaches code wise. none appear to be a 100 hundred percent fix, ex:

The DirectoryWatcher implements
AbstractResourceWatcher to monitor a
specified directory.

Code found here twit88.com develop-a-java-file-watcher

one problem encountered was If I copy
a large file from a remote network
source to the local directory being
monitored, that file will still show
up in the directory listing, but
before the network copy has completed.
If I try to do almost anything non
trivial to the file at that moment
like move it to another directory or
open it for writing, an exception will
be thrown because really the file is
not yet completely there and the OS
still has a write lock on it.

found on the same site, further below.

How the program works It accepts a ResourceListener class, which is FileListener. If a change is detected in the program a onAdd, onChange, or onDelete event will be thrown and passing the file to.

will keep searching for more solutions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文