我已经有几个月没有用Java编程了,我有一个小应用程序可供我自己使用。我被困在ResourceBundle上了!我记得它非常容易使用,所以我想我的问题是很容易解决的,但需要新的眼睛:)
Locale frLocale = new Locale("fr","CA");
Locale enLocale = new Locale("en","CA");
String dir = System.getProperty("user.dir");
System.out.println("current dir = " + dir); // current dir = D:\RP1.8.2
Path p = FileSystems.getDefault().getPath(dir,"test.properties");
System.out.println("Path : "+p.toAbsolutePath().toString()); // Path : D:\RP1.8.2\test.properties
boolean exists = Files.exists(p);
System.out.println("File found: "+exists); // File found: true
ResourceBundle bundle = ResourceBundle.getBundle(p.toAbsolutePath().toString());
/* Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name D:\RP1.8.2\test.properties, locale fr_CA */
String title = bundle.getString("main.title");
System.out.println("Titre = "+title);所以我的属性文件存在,但是ResourceBundle找不到它.真奇怪。在用户目录中有3个文件: test.properties、test_fr.properties和test_fr_CA.properties
我有什么线索吗?谢谢
发布于 2018-11-28 23:08:10
getBundle的参数不是文件名,实际上也不能是文件名。ResourceBundle文件是应用程序资源,需要与编译的类放在同一个位置。(实际上,ResourceBundle可以是实际的Java类,而不是属性文件。)例如:
build
└─classes
└─com
└─example
└─myapp
├─MyWindow.class
├─MyDataImporter.class
├─test.properties
├─test_fr.properties
└─test_fr_CA.properties代码:
ResourceBundle bundle = ResourceBundle.getBundle("com.example.myapp.test");发布于 2018-11-28 17:40:49
如果这是一个maven项目,我会右键单击该项目,然后在"M2工具“下选择generate工件(检查更新),然后单击”“。
https://stackoverflow.com/questions/53523391
复制相似问题