如何创建 .INI 文件来存储 Java 中的一些设置?

发布于 2024-07-07 06:48:36 字数 310 浏览 7 评论 0原文

我想创建一个 ini 文件来存储我的应用程序的一些设置。 找到 jar 文件所在的位置并在那里创建一个 ini 文件是一个好主意吗? 如果是,那么如何找到jar文件的位置?

但如果您知道针对此类问题的更好解决方案,我想听听其中的一些。

编辑:我使用的是 Mac,我想在 Windows 中运行相同的应用程序。 我可以在 System.getProperty("user.home") 目录中写入一些内容,但如果用户决定删除该应用程序,我想保持系统干净。 有没有更好的方法来存储设置文件,例如与应用程序位于同一目录中?

I want to create an ini file to store some settings for my application. Is it a good idea to find where the jar file is located and create an ini file there? If yes, then how can I find the location of the jar file?

But if you know a better solution for something like this, I would like to hear some of them.

EDIT: I'm using mac and I want to run the same application in windows. I could write something in the System.getProperty("user.home") directory, but I want to keep the system clean, if the user decides to remove the app. There is no a better way to store the settings file, for example in the same directory with the application?

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

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

发布评论

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

评论(6

上课铃就是安魂曲 2024-07-14 06:48:36

您可以使用类加载器找到您的应用程序目录。 请参阅:Java:查找应用程序目录。 使用 .properties 文件而不是 .INI 文件 - 您可以通过 属性 类。

正如其他人所指出的,您不应将用户设置写入应用程序目录。 如果用户没有应用程序目录的写权限怎么办? 如果您的应用程序同时被同一系统上的多个用户使用怎么办? 即使在 Windows 上,这两种情况也并不罕见。

您可能仍想从应用程序目录加载一些设置 - 也许管理员已在那里配置了默认设置。

常见的约定是将用户设置保存到用户的主目录:

/home/user/.eclipse
C:\Documents and Settings\User\.eclipse

虽然这意味着您可能会留下杂散文件,但如果用户重新安装应用程序,这可能会很有帮助。 在自述文件中记录这些内容。 以下是创建和获取目录引用的方法:

public static File getSettingsDirectory() {
    String userHome = System.getProperty("user.home");
    if(userHome == null) {
        throw new IllegalStateException("user.home==null");
    }
    File home = new File(userHome);
    File settingsDirectory = new File(home, ".myappdir");
    if(!settingsDirectory.exists()) {
        if(!settingsDirectory.mkdir()) {
            throw new IllegalStateException(settingsDirectory.toString());
        }
    }
    return settingsDirectory;
}

在类 UNIX 操作系统上,以句点(“.myappdir”)开头的目录名将使该目录隐藏。 在 Windows 上,它将位于我的文档下方,因此用户将看不到该目录,除非他们去查找它。

You can locate your application directory using the ClassLoader. See: Java: finding the application directory. Rather than an .INI file, use a .properties file - you can load and save this via the Properties class.

As others have noted, you should not write user settings to your application directory. What if the user does not have write access to the application directory? What if your application is being used by multiple users on the same system at the same time? Neither of these situations are unusual, even on Windows.

You might still want to load some settings from the application directory - perhaps the administrator has configured default settings there.

A common convention is to save user settings to the user's home directory:

/home/user/.eclipse
C:\Documents and Settings\User\.eclipse

Although this means you might leave stray files behind, this can be beneficial if the user re-installs the app. Document such things in a README. Here is how to create and get a reference to the directory:

public static File getSettingsDirectory() {
    String userHome = System.getProperty("user.home");
    if(userHome == null) {
        throw new IllegalStateException("user.home==null");
    }
    File home = new File(userHome);
    File settingsDirectory = new File(home, ".myappdir");
    if(!settingsDirectory.exists()) {
        if(!settingsDirectory.mkdir()) {
            throw new IllegalStateException(settingsDirectory.toString());
        }
    }
    return settingsDirectory;
}

On unix-like operating systems, starting the directory name with a period (".myappdir") will make the directory hidden. On Windows, it will be located below My Documents, so users will not see the directory unless they go looking for it.

行至春深 2024-07-14 06:48:36

如果设置仅由您的应用程序编写(而不是手动编辑),请考虑使用 首选项 API

If the settings are only written by your application (rather than edited manually), consider using the Preferences API.

清醇 2024-07-14 06:48:36

您不应将临时文件存储在应用程序的安装目录中。 请记住,运行应用程序的用户可能没有对该目录的写访问权限。 放置此类内容的最安全位置是 C:\Documents and Settings\username\Application Data\ApplicationName 文件夹(根据需要调整名称)。

不过,也就是说,我可能会将此类内容存储在注册表中,而不是他们计算机上的文件。 (但是,那只是我。)

You should not be storing temp files in the install directory of an application. Remember, the user running the application may not have write access to that directory. The safest place to put stuff like that is in C:\Documents and Settings\username\Application Data\ApplicationName folder (adjusting the name as necessary).

That said, however, I would probably store that type of stuff in the registry instead of a file on their computer. (But, that's just me.)

夏夜暖风 2024-07-14 06:48:36

通常,Java 程序员不使用 .ini 文件,而是使用 .properties 文件(不同格式)。 如果您这样做,您可以使用 java.lang.Properties 类作为一个很好的编程包装器。

虽然您可以通过在类的 .class 成员上调用 getProtectionDomain().getCodeSource().getLocation() 来获取 jar 文件的位置,但我建议您这样做做这个。

我会将文件写入 System.getProperty("user.home") 目录 - 用户的主目录,或者如果它确实是临时的,则 System.getProperty("java.io.tmpdir")

Typically Java programmers don't use .ini files, but .properties files (different format). You can use the java.lang.Properties class as a nice programmatic wrapper if you do.

While you can get the location of your jar file by calling getProtectionDomain().getCodeSource().getLocation() on your class's .class member, I do not recommend that you do this.

I would instead write the file to the System.getProperty("user.home") directory - the users' home directory, or if it is truly temporary, System.getProperty("java.io.tmpdir")

莳間冲淡了誓言ζ 2024-07-14 06:48:36

这取决于你的ini在正常情况下是否需要人类可读/可写。 如果没有,您可以使用属性文件而不是 ini 文件,并将其存储在“user”目录中。

至于查找 jar 文件,您必须找到已知从 jar 加载的类的 ClassLoader,检查它是否是适当类型的 ClassLoader(即它确实是从jar),您可以从中提取路径。 如果这确实是您想要的,我可能可以找出执行此操作的代码。 我不一定会推荐它。

编辑 user.home 属性将为您提供可以安全使用的用户目录。

It depends whether your ini needs to be human readable/writable under normal circumstances. If not, you can use a properties file rather than an ini file, and store it in the "user" directory.

As for finding the jar file, you would have to find the ClassLoader for a class known to be loaded from the jar, check that it was the appropriate type of ClassLoader (ie that it's really been loaded from a jar), and you can extract the path from that. I can probably dig out the code to do this if that's really what you want. I wouldn't necessarily recommend it.

EDIT The user.home property will give you the user directory, which you can safely use.

萌辣 2024-07-14 06:48:36

使用 .properties 文件代替 INI 文件的想法很好。 另外,如果您在那里存储一些敏感数据,您可以考虑对其进行加密。 看看这个:

https://www.owasp.org/index.php/How_to_encrypt_a_properties_file

或者这样:

加密和解密java中的属性文件值

The idea with the .properties file instead of the INI file is good. Also, if you store some sensitive data in there, you may consider encrypting it. Check this out:

https://www.owasp.org/index.php/How_to_encrypt_a_properties_file

or this:

encrypt and decrypt property file value in java

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