如何创建一个 .INI 文件来存储 Java 中的一些设置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/193474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create an .INI file to store some settings in Java?
提问by Lipis
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?
我想创建一个 ini 文件来存储我的应用程序的一些设置。找到 jar 文件所在的位置并在那里创建一个 ini 文件是个好主意吗?如果是,那么如何找到 jar 文件的位置?
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?
编辑:我使用的是 mac,我想在 Windows 中运行相同的应用程序。我可以在 System.getProperty("user.home") 目录中写一些东西,但我想保持系统干净,如果用户决定删除应用程序。有没有更好的方法来存储设置文件,例如在与应用程序相同的目录中?
采纳答案by McDowell
You can locate your application directory using the ClassLoader. See: Java: finding the application directory. Rather than an .INIfile, use a .propertiesfile - you can load and save this via the Propertiesclass.
您可以使用 ClassLoader 定位您的应用程序目录。请参阅:Java:查找应用程序目录。使用.properties文件而不是.INI文件- 您可以通过Properties类加载和保存它。
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.
正如其他人所指出的,您不应将用户设置写入应用程序目录。如果用户对应用程序目录没有写权限怎么办?如果您的应用程序被同一系统上的多个用户同时使用怎么办?这些情况都不是不寻常的,即使在 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.
在类 Unix 操作系统上,以句点(“.myappdir”)开头的目录名称将使目录隐藏。在 Windows 上,它将位于My Documents下方,因此用户将不会看到该目录,除非他们去寻找它。
回答by BoltBait
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).
您不应将临时文件存储在应用程序的安装目录中。请记住,运行该应用程序的用户可能没有对该目录的写访问权限。放置此类内容的最安全位置是 C:\Documents and Settings\username\Application Data\ApplicationName 文件夹(根据需要调整名称)。
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.)
话虽如此,但是,我可能会将这种类型的东西存储在注册表中,而不是他们计算机上的文件中。(但是,那只是我。)
回答by Draemon
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.
这取决于您的 ini 在正常情况下是否需要人类可读/可写。如果没有,您可以使用属性文件而不是 ini 文件,并将其存储在“用户”目录中。
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 reallybeen 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.
至于查找 jar 文件,您必须找到已知从 jar 加载的类的 ClassLoader,检查它是否是适当类型的 ClassLoader(即它确实是从 jar 加载的),然后您可以提取从那个路径。如果这真的是你想要的,我可能会挖掘出代码来做到这一点。我不一定会推荐它。
EDITThe user.home property will give you the user directory, which you can safely use.
编辑user.home 属性将为您提供可以安全使用的用户目录。
回答by Chris Boran
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.
通常,Java 程序员不使用 .ini 文件,而是使用 .properties 文件(不同格式)。如果您这样做,您可以使用 java.lang.Properties 类作为一个很好的编程包装器。
While you canget the location of your jar file by calling getProtectionDomain().getCodeSource().getLocation() on your class's .class member, I do notrecommend that you do this.
虽然您可以通过对类的 .class 成员调用 getProtectionDomain().getCodeSource().getLocation()来获取 jar 文件的位置,但我不建议您这样做。
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")
我会将文件写入 System.getProperty("user.home") 目录 - 用户的主目录,或者如果它真的是临时的, System.getProperty("java.io.tmpdir")
回答by Dan Dyer
If the settings are only written by your application (rather than edited manually), consider using the Preferences API.
如果设置仅由您的应用程序编写(而不是手动编辑),请考虑使用Preferences API。
回答by Radu Murzea
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:
用 .properties 文件代替 INI 文件的想法很好。此外,如果您在其中存储一些敏感数据,您可以考虑对其进行加密。看一下这个:
https://www.owasp.org/index.php/How_to_encrypt_a_properties_file
https://www.owasp.org/index.php/How_to_encrypt_a_properties_file
or this:
或这个: