如何创建.INI文件以在Java中存储一些设置? [英] How to create an .INI file to store some settings in Java?

查看:256
本文介绍了如何创建.INI文件以在Java中存储一些设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

编辑:我正在使用mac,我想在Windows中运行相同的应用程序。我可以在System.getProperty(user.home)目录中写一些东西,但是如果用户决定删除应用程序,我想保持系统清洁。没有更好的方法来存储设置文件,例如与应用程序在同一目录中?

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?

推荐答案

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

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.

正如其他人所说,你不应将用户设置写入应用程序目录。如果用户没有对应用程序目录的写访问权,该怎么办?如果您的应用程序同时被同一系统上的多个用户使用该怎么办?这些情况都不常见,即使在Windows上也是如此。

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

虽然这意味着您可能会遗留杂散文件,但如果用户重新安装该应用程序,这将非常有用。在README中记录这些内容。以下是如何创建和获取对目录的引用:

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;
}

在类似unix的操作系统上,用句点启动目录名称( .myappdir)将隐藏目录。在Windows上,它将位于我的文档下面,因此除非用户查找目录,否则用户将看不到该目录。

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.

这篇关于如何创建.INI文件以在Java中存储一些设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆