Talk about the difference between ResourceBundle and properties reading configuration files

  • 2021-11-02 00:59:25
  • OfStack

java. util. ResourceBundle and java. util. properties read configuration file difference

Both classes read files in properties format, while Properties can also be used to write files.

Properties is treated as a mapping table, and this class represents a persistent set of attributes that inherits from HashTable.

ResourceBundle is also essentially a mapping, but it provides internationalized functionality.

Suppose the computer is set up in Chinese mainland and the language is Chinese

Then when you get the value of abc variable from ResourceBundle (resource constraint name is base), ResourceBundle will search one after another

base_zh_CN_abc.properties

base_zh_CN.properties

base_zh.properties

base.properties

File until abc is found

Accordingly, in Britain, you will look for base_en_GB_abc. properties, etc.

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

In addition, in. properties, characters such as Chinese cannot be used directly, but the form of\ uxxxx must be transferred through native2ascii

Attachment:

1. Coding issues:

Whatever the default encoding of the system, ResourceBundle uses iso 8859-1 encoding when reading properties files.

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

2. Usage:

ResourceBundle:


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

Properties:


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

Reading Properties File by ResourceBundle and Handling Garbled Code


package read; 
import java.util.ResourceBundle;
/**
 *  Properties 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 properties The name of will do 
			errorResouce = ResourceBundle.getBundle("errorMessage");
		}
		return errorResouce;
	} 	
}
===============================================
 
package util; 
import java.io.UnsupportedEncodingException; 
/**
 * 
 * @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 Attributes in files 
E01010024= Query data exception! 
=============================================================================
package www.man.comService;
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){
try{
return util.StringHanlder.transformCodeIso8859Style(getErrorResource().getString(key),"utf-8");
}catch(Exception e){
e.printStackTrace();return "";
}
}}

Related articles: