访问 .jar 之外的属性文件? [英] Accessing properties files outside the .jar?
问题描述
我有一个 .jar 文件要放在一起.我想创建一个非常简单的 .properties 文件,其中包含可配置的内容,例如用户名 &其他东西,以便他们可以手动编辑,而不必包含 GUI 编辑器.
I have a .jar file I'm putting together. I want to create a really really simple .properties file with configurable things like the user's name & other stuff, so that they can hand-edit rather than my having to include a GUI editor.
我想做的是能够按以下顺序进行搜索:
What I'd like to do is to be able to search, in this order:
- 一个指定的属性文件(
args[0]
) - 当前目录中的MyApp.properties(调用Java的目录)
- 用户目录中的 MyApp.properties(user.home 系统属性?)
- MyApp.properties 在应用程序 .jar 的存储目录中
我知道如何访问 #1 和 #3(我认为),但是如何在运行时确定 #2 和 #4?
I know how to access #1 and #3 (I think), but how can I determine at runtime #2 and #4?
推荐答案
#2 是 "user.dir" 系统属性.#3 是user.home"属性.
#2 is the "user.dir" system property. #3 is the "user.home" property.
无论您如何处理,#4 都有些麻烦.如果您从不在系统类路径上的 JAR 加载了一个类,这里有一种替代技术.
#4 is a bit of a kludge no matter how you approach it. Here's an alternate technique that works if you have a class loaded from a JAR not on the system classpath.
CodeSource src = MyClass.class.getProtectionDomain().getCodeSource();
if (src != null) {
URL url = new URL(src.getLocation(), "MyApp.properties");
...
}
else {
/* Fail... */
}
这篇关于访问 .jar 之外的属性文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!