Talk about the difference between resourcebundle and properties reading configuration files


java. util. Resourcebundle and java util. Properties read configuration file differences

These two classes both read files in the format of properties, and properties can also be used to write files.

Properties is treated as a mapping table, and this class represents a persistent property set. It inherits the hashtable class.

Resourcebundle is also a mapping in nature, but it provides internationalization.

Suppose the computer is set in Chinese Mainland and the language is Chinese

When you get the value of ABC variable from resourcebundle (resource constraint name is base), resourcebundle will search successively

File until ABC is found

Correspondingly, in the UK, we will find base_ en_ GB_ abc. Properties, etc.

Therefore, you only need to provide resource files in different languages without changing the code to achieve the goal of internationalization.

In addition, in In properties, Chinese and other words cannot be used directly. Instead, native2ascii is used to multiply \uxxxx


1. coding problem:

No matter what the default encoding of the system is, the resourcebundle uses iso8859-1 encoding when reading the properties file.

Therefore, if a properties file containing Chinese is written in the system with the default encoding of GBK, it must be converted to the encoding of GBK format when it is read through the resourcebundle, otherwise it cannot be recognized correctly.

2. usage:


ResourceBundle conf= ResourceBundle.getBundle("config/fnconfig/fnlogin");
String value= conf.getString("key");


Properties prop = new Properties();
try { InputStream is = getClass().getResourceAsStream("");
//Or directly prop load(new FileInputStream("c:/"));
if (is != null) { is.close();
} } catch (Exception e) { System.out.println( "file " + "" + " not found!\n" + e); } String value= prop.getProperty("key").toString();

Resourcebundle reading properties file and garbled code processing

package read; 
import java.util.ResourceBundle;
 *Property file factory class
 * @author W
 * @version V1.0
 * @date 2013-4-17
public interface ReadPropertiesFactory {
	public ResourceBundle getErrorResource();	

package read; 
import java.util.ResourceBundle;
 * @author 
 * @version V1.0
 * @date 2013-5-13
public class ReadPropertiesFactoryImpl implements ReadPropertiesFactory {
	private ResourceBundle errorResouce;
	public ResourceBundle getErrorResource() {
		if(errorResouce == null){
                     //Just read the name of the properties
			errorResouce = ResourceBundle.getBundle("errorMessage");
		return errorResouce;
package util; 
 * @author 
 * @version V1.0
 * @date 2013-4-17
public class StringHanlder {
	public static String transformCodeIso8859Style(String code , String codeStyle) throws UnsupportedEncodingException{
		return new String(code.getBytes("ISO-8859-1"),codeStyle);
	public static String transformCodeUtf8Style(String code , String codeStyle) throws UnsupportedEncodingException{
		return new String(code.getBytes("utf-8"),codeStyle);
errorMessage. Properties in the properties file
E01010024= abnormal query data!
import java.util.ResourceBundle;
import read.ReadPropertiesFactoryImpl;
public class TestService {public static void main(String[] args) {
String a= TestService.getErrorValue("E01010070");System.out.println(a);}
private static  ResourceBundle getErrorResource() {
ReadPropertiesFactoryImpl readPropertiesFactory =new ReadPropertiesFactoryImpl();
return readPropertiesFactory.getErrorResource();
public  static String getErrorValue(String key){
return util.StringHanlder.transformCodeIso8859Style(getErrorResource().getString(key),"utf-8");
}catch(Exception e){
e.printStackTrace();return "";

The above is my personal experience. I hope I can give you a reference, and I hope you can support developeppaer.

Recommended Today

A JS package source code of Ajax auto completion function [support Chinese] page 1/2

Screenshots Pagination Internationalization Rich format Text selection Source code download There’s a problem that can’t be solved by experts. I don’t know where to change! expect Asp+ajax autocompant ASP full demo Asp+ajax source code download Page turning and other extended functions are being improved Copy codeThe codes are as follows: For example, new capxous AutoComplete(“wiki”, function() {                 return “autocomplete3.asp?typing=” + escape(this.text.value);  […]