java国际化中ResourceBoundle和properties类:
1、properties经常用来缓存系统的 配置文件,是基于map的
存储方式, 配置文件可以直接从输入流中加载或配置改变时
保存到流中。
2、ResourceBoundle用来加载和存取取系统的中英文资源码,
eclipse中有专门编辑资源文件的插件。
Java代码
package Util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;
public class TestApp
{
public static void initConfig()
{
String filepath = System.getProperty("user.dir") + File.separator + "TestApp.cfg";
try
{
FileInputStream inputStream = new FileInputStream(filepath);
Properties properties = new Properties();
try
{
properties.load(inputStream);
AppConfig.language = properties.getProperty("language");
AppConfig.AUTO_SAVE_PATH = properties.getProperty("autoSavePath");
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("加载配置文件失败!");
}
}
public static void main(String[] args)
{
TestApp.initConfig();
if (AppConfig.language.equals("zh"))
{
Resource.resouces = ResourceBundle.getBundle("Util.TestAppRes_zh", Locale.CHINA);
System.out.println(Resource.getResource("key1"));
System.out.println(Resource.getResource("key2"));
System.out.println(Resource.getResource("key3"));
}
System.out.println(AppConfig.AUTO_SAVE_PATH);
}
}
//--------------------------------------------------------------------
public class AppConfig
{
public static String language = "";
public static String AUTO_SAVE_PATH = "";
}
//--------------------------------------------------------------------import java.util.ResourceBundle;
public class Resource
{
public static ResourceBundle resouces = null;
public static String getResource(String key)
{
try
{
String value = resouces.getString(key);
return value;
}
catch (Exception e)
{
System.out.println("error:" + key);
return key;
}
}
}
package Util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;
public class TestApp
{
public static void initConfig()
{
String filepath = System.getProperty("user.dir") + File.separator + "TestApp.cfg";
try
{
FileInputStream inputStream = new FileInputStream(filepath);
Properties properties = new Properties();
try
{
properties.load(inputStream);
AppConfig.language = properties.getProperty("language");
AppConfig.AUTO_SAVE_PATH = properties.getProperty("autoSavePath");
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("加载配置文件失败!");
}
}
public static void main(String[] args)
{
TestApp.initConfig();
if (AppConfig.language.equals("zh"))
{
Resource.resouces = ResourceBundle.getBundle("Util.TestAppRes_zh", Locale.CHINA);
System.out.println(Resource.getResource("key1"));
System.out.println(Resource.getResource("key2"));
System.out.println(Resource.getResource("key3"));
}
System.out.println(AppConfig.AUTO_SAVE_PATH);
}
}
//--------------------------------------------------------------------
public class AppConfig
{
public static String language = "";
public static String AUTO_SAVE_PATH = "";
}
//--------------------------------------------------------------------import java.util.ResourceBundle;
public class Resource
{
public static ResourceBundle resouces = null;
public static String getResource(String key)
{
try
{
String value = resouces.getString(key);
return value;
}
catch (Exception e)
{
System.out.println("error:" + key);
return key;
}
}
}
中文资源吗文件:TestAppRes_zh.properties
内容如下: