java 国际化 properties_java国际化中ResourceBoundle和properties类

java国际化中ResourceBoundle和properties类:

1、properties经常用来缓存系统的 配置文件,是基于map的

存储方式, 配置文件可以直接从输入流中加载或配置改变时

保存到流中。

2、ResourceBoundle用来加载和存取取系统的中英文资源码,

eclipse中有专门编辑资源文件的插件。

Java代码 icon_copy.gificon_star.png

spinner.gif

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

内容如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值