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
base_zh_CN_abc.properties
base_zh_CN.properties
base_zh.properties
base.properties
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
Attachment:
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:
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();
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;
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 in the properties file
E01010024= abnormal query data!
=============================================================================
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 "";
}
}}
The above is my personal experience. I hope I can give you a reference, and I hope you can support developeppaer.