文件目录结构:
- src
- res
- GetConfByJar.java
- user.properties
- res
源码展示:
import java.util.ResourceBundle;
import java.util.Set;
public class GetConfByJar {
public static void main(String[] args) {
getConf();
}
/**
* 获取jar包里面的资源,返回bundle对象
*
* @return
*/
public static ResourceBundle getConf() {
ResourceBundle bundle = ResourceBundle.getBundle("user");
sysToConsole(bundle);
return bundle;
}
/**
* 输出打印
*
* @param rb
*/
public static void sysToConsole(ResourceBundle rb) {
if (rb != null) {
Set<String> keys = rb.keySet();
for (String key : keys) {
System.out.println(key + ":" + rb.getString(key));
}
}
}
}
user.properties
userName = \u4e2d\u6587\u6635\u79f0
enName = English Name
age = 25
gender = male
address = China
控制台输出
将项目打成jar包,使用 java -cp xxx.jar xxx.xxx.GetConfByJar执行。例如:
运行: D:>java -cp test.jar res.GetConfByJar
结果:
address:China
gender:male
enName:English Name
userName:中文昵称
age:25