[英]Java ResourceBundle
I haven't programmed in Java for months and I have a small application to develop for my own usage. 我已经几个月没有使用Java编程了,但是我有一个小应用程序可以开发供自己使用。 I am stuck on ResourceBundle ! 我被困在ResourceBundle上! I remember it to be very easy to use, so I guess my problem is something very easy to solve but needs new eyes :) 我记得它非常易于使用,所以我想我的问题很容易解决,但是需要新的眼睛:)
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);
So my properties file exists but the ResourceBundle doesnt find it ... weird. 所以我的属性文件存在,但是ResourceBundle没有找到它……很奇怪。 In the user directory i have 3 files: test.properties, test_fr.properties and test_fr_CA.properties 在用户目录中,我有3个文件:test.properties,test_fr.properties和test_fr_CA.properties
Any clue for me ? 对我有什么线索吗? Thanks 谢谢
The argument to getBundle is not a file name, and in fact must not be a file name. getBundle的参数不是文件名,并且实际上一定不能是文件名。 ResourceBundle files are application resources, and need to be placed in the same location as your compiled classes. ResourceBundle文件是应用程序资源,需要与编译的类放在相同的位置。 (In fact, a ResourceBundle can be an actual Java class instead of a properties file.) For example: (实际上,ResourceBundle可以是实际的Java类,而不是属性文件。)例如:
build
└─classes
└─com
└─example
└─myapp
├─MyWindow.class
├─MyDataImporter.class
├─test.properties
├─test_fr.properties
└─test_fr_CA.properties
Code: 码:
ResourceBundle bundle = ResourceBundle.getBundle("com.example.myapp.test");
如果它是一个Maven项目,那么我将右键单击该项目,然后在“ M2工具”下选择“生成工件(检查更新)”,然后单击“生成OSGI捆绑包”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.