一、使用
属性文件:
config.properties
属性文件内容如下:
name=jack
password=123456
在java文件里面读取属性文件,可以使用ResourceBundle,如下:
ResourceBundle rs = ResourceBundle.getBundle("config");
String name = rs.getString("name");
String passwd = rs.getString("password");
System.out.println("name="+name);
Systme.out.println("password="+passwd);
输出结果如下:
name=jack
password=123456
二、Exception
如果遇到类似如下Exception:
Solve java.util.MissingResourceException: Can't find bundle for base name com...config, locale zh_CN
at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:836)
at java.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 specific locale. You may be baffled why java keeps complaining it can't find a properties file that is right there. A few things to keep in mind when debugging this type of errors:- These resource properties files are loaded by classloader, similar to java classes. So you need to include them in your runtime classpath.
- These resources have fully-qualified-resource-name, similar to a fully-qualified-class-name, excerpt you can't import a resource into your java source file. Why? because its name takes the form of a string.
ResourceBundle.getBundle("config")
tells the classloader to load a resource named"config"
with default package (that is, no package). It does NOT mean a resource in the current package that has the referencing class.
ResourceBundle.getBundle("com.cheng.scrap.config")
tells the classloader to load a resource named"config"
with package"com.cheng.scrap."
Its fully-qualified-resource-name is"com.cheng.scrap.config"
For instance, you have a project like
C:/ws/netbeans5/scrap>
| build.xml
+---build
| /---classes
| /---com
| /---cheng
| /---scrap
| Scrap.class
|
+---src
| /---com
| /---cheng
| /---scrap
| config.properties
| Scrap.java
For this statement in Scrap.java: ResourceBundle config = ResourceBundle.getBundle("config");
to work, you will need to cp src/com/cheng/scrap/config.properties build/classes/
such that config.properties
is directly under classes
, and at the same level as com
. Alternatively, you can put config.properties
into a config.jar
such that config.properties
is at the root of config.jar
without any subdirectories, and include config.jar
in the classpath.
For this statement in Scrap.java: ResourceBundle config = ResourceBundle.getBundle("com.cheng.scrap.config");
to work, you will need to cp src/com/cheng/scrap/config.properties build/classes/
com/cheng/scrap/
such that config.properties
is directly under classes
/
com/cheng/scrap/
, and at the same level as scrap
. Alternatively, you can put com/cheng/scrap/
config.properties
(along with the long subdirectories) into a config.jar
, and include
config.jar
in the classpath.
You may be wondering why it is made so confusing? The benefits are two-fold, as I see it:
- Location transparency. At runtime, config.properties is NOT a file, it's just a a loadable resource. config.properites may not exist in your project at all, and the person who wrote Scrap.java may have never seen this resource. A URLClassLoader can find it in a network path or URL at runtime. This is especially important for server-side components such as EJB, Servlet, JSP, etc, who are normally not allowed to access file systems. When you ask classloaders for a resource, its physical location becomes irrelevant.
- Namespace mechanism. Having a package allows multiple packages to have resources with the same short name without causing conflicts. This is no different from java packages and xml namespaces.
1、将属性文件(这里为config.properties)和读取文件在一起;
2、
ResourceBundle.getBundle(String arg0)中的参数arg0必须包含属性文件的完整路径。
[参考:http://hi.baidu.com/mircoteam/blog/item/a2a0dc3e6b4bf0f8828b139e.html]
三、属性文件的路径
正如上述所说的,属性文件和读取的文件要放在一起,如果要分开这两个文件呢?上面第一点有提到:
These resource properties files are loaded by classloader......,那么我们自己可以自己建立一个目录,将属性文件放到其下,再将该目录设置为classloader加载的目录(加入类路径中)则可,如下
:
1、在项目下建一文件夹,路径随意,名字任意(这为:properties)。
2、(Eclipse中)选择项目->
Properties->java Build Path->Libraries->Add Class Folder,将properties文件加入类路径即可(或者手动在.classpath文件中加入:<classpathentry kind="lib" path="properties"/>)。3、然后直接用
ResourceBundle.getBundle("config");则可读取properties/config.properties文件的内容。
[参考:http://verran.javaeye.com/blog/44357]