今天学习了java ResourceBundle类的内容,但一直还有一个问题没有解决,就是它读取资源文件路径的问题.
以下是我的代码:
1
package
test.resource;
2
3 import java.util.Locale;
4 import java.util.ResourceBundle;
5
6 public class Test {
7 public static void main(String args[]) {
8 Test test = new Test();
9 ResourceBundle messages = test.loadResource();
10 System.out.println(messages.getObject( " name " ));
11 }
12
13 private ResourceBundle loadResource() {
14 Locale local = Locale.getDefault();
15 ResourceBundle messages = ResourceBundle.getBundle( " messages " , local,
16 this .getClass().getClassLoader());
17 return messages;
18 }
19 }
我的两个资源文件messages_en_US.properties,messages_zh_CN.properties都在当前类路径下面,可是 这样的话运行程序它就找不到资源文件,会报错:Can't find bundle for base name messages, locale zh_CN
2
3 import java.util.Locale;
4 import java.util.ResourceBundle;
5
6 public class Test {
7 public static void main(String args[]) {
8 Test test = new Test();
9 ResourceBundle messages = test.loadResource();
10 System.out.println(messages.getObject( " name " ));
11 }
12
13 private ResourceBundle loadResource() {
14 Locale local = Locale.getDefault();
15 ResourceBundle messages = ResourceBundle.getBundle( " messages " , local,
16 this .getClass().getClassLoader());
17 return messages;
18 }
19 }
我必需把两个资源文件copy到classes根目录下才行,我想资源文件是可以放在当前类路径下面的,可是不太清楚如何读取出来,ResourceBundle.getBundle("messages", local,
this.getClass().getClassLoader());方法好像只能读根路径下的资源,请教各位了!