resourcebundleresourcebundle找不到文件
本文目录一览:
- 1、resourcebundle.getBundle()如何读取webroot文件下的属性文件
- 2、PropertyResourceBundle 什么意思
- 3、Java中resourceBundle和Properties的区别
- 4、springmvc中怎么从配置文件中读取信息
- 5、java常用的配置文件有哪些
resourcebundle.getBundle()如何读取webroot文件下的属性文件
ResourceBundle rb=ResourceBundle.getBundle("属性文件路径");
//将文件的key取出
EnumeratiONString em=rb.getKeys();
//存放属性文件的键值对
HashMapString, String hm=new HashMapString, String();
//根据key取值
while(em.hasMoreElements()){
String key=em.nextElement();
String value=rb.getString(key);
hm.put(key, value);
}
//属性文件的内容全部在map中了
PropertyResourceBundle 什么意思
PropertyResourceBundle 是 ResourceBundle 的一个具体子类,它使用属性文件中的静态字符串集合来管理语言环境资源。有关资源包的更多信息,请参阅 ResourceBundle。有关属性文件的更多信息,特别是有关字符编码的信息,请参阅 Properties。
与其他资源包类型不同,不能为 PropertyResourceBundle 创建子类。相反,要提供含有资源数据的属性文件。ResourceBundle.getBundle 将自动查找合适的属性文件并创建引用该文件的 PropertyResourceBundle。有关搜索和实例化策略的完整描述,请参阅 ResourceBundle.getBundle。
下面的示例显示了资源包系列中具有基本名称 "MyResources" 的成员。文本定义了包 "MyResources_de",这是该包系列中的德语成员。该成员基于 PropertyResourceBundle,因此文本就是文件 "MyResources_de.properties" 的内容(一个相关的 示例显示了如何将包添加到该系列中,该系列作为 ListResourceBundle 的子类来实现)。本示例的键形式为 "s1" 等等。实际的键完全取决于您的选择,只要它们与在程序中使用的、用于从包中检索对象的键相同即可。键是区分大小写的。
# MessageFormat pattern
s1=Die Platte \"{1}\" enthält {0}.
# location of {0} in pattern
s2=1
# sample disk name
s3=Meine Platte
# first ChoiceFormat choice
s4=keine Dateien
# second ChoiceFormat choice
s5=eine Datei
# third ChoiceFormat choice
s6={0,number} Dateien
# sample date
s7=3. März 1996
Java中resourceBundle和Properties的区别
一般来说,ResourceBundle类通常是用于针对不同的语言来使用的属性文件。
而如果你的应用程序中的属性文件只是一些配置,并不是针对多国语言的目的。那么使用Properties类就可以了。
通常可以把这些属性文件放在某个jar文件中。然后,通过调用class的getResourceAsStream方法,来获得该属性文件的流对象,再用Properties类的load方法来装载。
示例如下:
Class TestLoad {
public static void main( String[] argv) {
InputStream is = TestLoad.class.getResourceAsSteam("myprops.properties");
Properties p = new Properties();
p.load(is);
System.out.println(p.get("MAIL_SERVER_HOSTNAME"));
}
}
关于ResourceBundle
有时候有些简单的配置文件可以没必要使用xml,其实ResourceBundle类就已经做的很好的。它甚至可以搜索到classpath里的jar文件中一些properties文件。
例如在jar文件中的根目录放置一个文件:test.properties,然后只要这个jar文件在classpath里。就可以使用这样的语句来获得一些属性:
ResourceBundle rb = ResourceBundle.getBundle("test");
String s = rb.getString("MQ_Server_Address");
System.out.println(s);
springmvc中怎么从配置文件中读取信息
在使用hibernate或者spring的时候,我们往往通过配置文件配置数据库连接属性。但这次项目中并没有用到hibernate和spring,只用到了struts2。要如何实现通过读取文件配置获取属性值呢?
方式一:ResourceBundle这个类可是实现读取properties文件来获取值
在java中:
public class ResourceBundleReader {
public final static Object initLock = new Object();
private final static String PROPERTIES_FILE_NAME = "property";
private static ResourceBundle bundle = null;
static {
try {
if (bundle == null) {
synchronized (initLock) {
if (bundle == null)
bundle = ResourceBundle.getBundle(PROPERTIES_FILE_NAME,Locale.CHINA);
}
}
} catch (Exception e) {
System.out.println("读取资源文件property_zh.properties失败!");
}
}
public static ResourceBundle getBundle() {
return bundle;
}
public static void setBundle(ResourceBundle bundle) {
bundle = bundle;
}
}
在.properties文件中:
driverName=com.mysql.jdbc.Driver
url=xxxxx/:3307/9zgame?
user=root
password=xxxxxx
文件名字为:property_zh.properties。后zh根据Locale.CHINA一致的,如果Locale.ENGLISH,则文件名为:property_en.properties
方式二:使用commons组件。
java常用的配置文件有哪些
项目中经常会需要读取配置文件(properties文件),给你总结了配置文件读取方法如下:
1、通过java.util.Properties读取
Java代码
Properties p=new Properties();
//p需要InputStream对象进行读取文件,而获取InputStream有多种方法:
//1、通过绝对路径:InputStream is=new FileInputStream(filePath);
//2、通过Class.getResourceAsStream(path);
//3、通过ClassLoader.getResourceAsStream(path);
p.load(InputStream is);
is.close();
p.getString(String(key))
2、通过java.util.ResourceBundle读取
Java代码
ResourceBundle rb=ResourceBundle.getBundle(packageName);
rb.getString(String key);
resourcebundle的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于resourcebundle找不到文件、resourcebundle的信息别忘了在万域城进行查找喔。
相关文章
发表评论
评论列表
- 这篇文章还没有收到评论,赶紧来抢沙发吧~