External file for strings, as opposed to hardcoding them

非 Y 不嫁゛ 提交于 2019-12-05 08:17:35
Moritz Petersen

Yes, you should use .properties files and access them using ResourceBundle.

Eclipse has a built-in refactoring that you may find useful.

Source->Externalise Strings

This creates a properties file with the strings stored as key/value pairs.

Behind the scenes it is using ResourceBundle

A B

I think if you do this it will not be specific to any IDE: See Load resources via a resources file for an overview. The 1st URL states this about where this class looks for the properties files.

See the ResourceBundle class.

The ResourceBundle will look first for classes and if not found, it will look for properties files. We don't have to specify file names, the ResourceBundle will construct the file name using the same mechanism used for classes. The file must have the extension .properties.

The ResourceBundle try to load the properties file from the current class-path.

If the properties are stored in sub-directory, use . instead of /.

Mike Bockus

Take a look at the Javadoc for ResourceBundle. The API provides a way to fetch strings for locale specific resources.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!