java中显示动态配置(ResourceBundle)
来源:互联网 发布:电信网络测速 编辑:程序博客网 时间:2024/05/17 09:00
在java中想要根据不同的环境设置不同的字符,可以使用下面的这种方法:
import java.text.MessageFormat;
import java.util.MissingResourceException
import java.util.ResourceBundle;
public class Messages {
}
编写messages.properties,上面的类会自动读取里面的变量:
如:
toolItemLoadCode
view
程序中使用:
toolItemLoadCode.setToolTipText(Messages.getString("view"));
在这里有一个要注意的问题,那就是messages.properties文件的放置位置,有的时候会出现下面的错误:
java.util.MissingResourceException
解决解释如下:
Solve java.util.MissingResourceException
atjava.util.ResourceBundle.throwMissingResourceExce
atjava.util.ResourceBundle.getBundleImpl(ResourceBundle.java:805)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:576)
You know java is looking for a properties file in a specificlocale.
- These resource properties files are loaded by classloader,similar to java classes.
So youneed to include them in your runtime classpath. - These resources have fully-qualified-resource-name, similar toa fully-qualified-class-name, excerpt you can't import a resourceinto your java source file.
Why? because its name takes the form of a string. ResourceBundle.getBundle("config")
tells theclassloader to load a resource named"config"
with default package (that is, nopackage).It does NOT mean aresource in the current package that has the referencingclass. ResourceBundle.getBundle("com.cheng.scrap.config")
tells the classloader to load a resource named"config"
with package"com.cheng.scrap."
Itsfully-qualified-resource-name is"com.cheng.scrap.config"
For instance, you have a project like
C:\ws\netbeans5\scrap>
|
+---build
|
|
|
|
|
|
+---src
|
|
|
|
|
For this statement in Scrap.java:ResourceBundle config =ResourceBundle.getBundle("config");
to work, you willneed to cp src\com\cheng\scrap\config.propertiesbuild\classes\
such that config.properties
is directly underclasses
, and at the same levelascom
. config.properties
into a config.jar
such that config.properties
is at the root ofconfig.jar
without anysubdirectories, and includeconfig.jar
in the classpath.
For this statement in Scrap.java:ResourceBundle config =ResourceBundle.getBundle("com.cheng.scrap.config");
to work, you will needto cpsrc\com\cheng\scrap\config.propertiesbuild\classes\
com\cheng\scrap\
such thatconfig.properties
is directlyunderclasses
\
com\cheng\scrap\
,and at the same level asscrap
. com\cheng\scrap\
config.properties
(along with the long subdirectories) into aconfig.jar
, andinclude
config.jar
in theclasspath.
You may be wondering why it is made soconfusing?
- Location transparency.
Atruntime, config.properties is NOT a file, it's just a a loadableresource. config.properites maynot exist in your project at all, and the person who wroteScrap.java may have never seen thisresource. A URLClassLoader canfind it in a network path or URL atruntime. This is especiallyimportant for server-side components such as EJB, Servlet, JSP,etc, who are normally not allowed to access filesystems. When you askclassloaders for a resource, its physical location becomesirrelevant. - Namespace mechanism.
Havinga package allows multiple packages to have resources with the sameshort name without causing conflicts. This is no different fromjava packages and xml namespaces.
- java中显示动态配置(ResourceBundle)
- JAVA 中 ResourceBundle 使用
- Java中使用ResourceBundle访问资源文件(properties文件)
- Java中使用ResourceBundle访问资源文件(properties文件)
- Java中使用ResourceBundle访问资源文件(properties文件)
- Java中ResourceBundle类的使用
- Java中ResourceBundle类的使用
- Java常用类库——国际化程序(Locale,ResourceBundle以及MessageFormat处理动态文本)
- java 资源包(ResourceBundle) 详解【1】
- java 资源包(ResourceBundle) 详解【2】
- java工具类(1)ResourceBundle
- Java资源绑定(ResourceBundle)示例
- JAVA中ResourceBundle和Properties类的区别
- springboot 中ResourceBundle 加载配置文件 报java.util.MissingResourceException
- java的ResourceBundle.
- 学习Java:ResourceBundle
- Java的ResourceBundle
- java.util.ResourceBundle
- 【机器学习系列】机器学习界大牛林达华推荐的书籍
- 51单片机多任务机制的实现策略
- XStream转换list、多个节点。
- unity项目总结-3
- .net程序安装时提示缺少Framework
- java中显示动态配置(ResourceBundle)
- 基本语言细节--POD类型
- 选择排序
- Visual C# 常见问题目录
- 常用排序算法——归并排序法
- No video issues for Sony BDP360
- android adb shell 命令大全
- Vim中如何全选并复制?
- TestNG中的DataProvider