ResourceBundle和ObjectInputStream、ObjectOutputStream使用详解

说的简单点,这个类的作用就是读取资源属性文件(properties),然后根据.properties文件的名称信息(本地化信息),匹配当前系统的国别语言信息(也可以程序指定),然后获取相应的properties文件的内容。
 
使用这个类,要注意的一点是,这个properties文件的名字是有规范的:一般的命名规范是:  自定义名_语言代码_国别代码.properties
如果是默认的,直接写为: 自定义名.properties
比如:
myres_en_US.properties
myres_zh_CN.properties
myres.properties
 
当在中文操作系统下,如果myres_zh_CN.properties、myres.properties两个文件都存在,则优先会使用myres_zh_CN.properties,当myres_zh_CN.properties不存在时候,会使用默认的myres.properties。
 
没有提供语言和地区的资源文件是系统默认的资源文件。
资源文件都必须是ISO-8859-1编码,因此,对于所有非西方语系的处理,都必须先将之转换为Java Unicode Escape格式。转换方法是通过JDK自带的工具native2ascii.
 
二、实例
 在JavaWEB中的应用
定义三个资源文件,放到src的根目录下面(必须这样,或者你放到自己配置的calsspath下面。
 
myres.properties
aaa=good 
bbb=thanks

myres_en_US.properties
aaa=good 
bbb=thanks

myres_zh_CN.properties
aaa=\u597d 
bbb=\u591a\u8c22
 
import java.util.Locale; 
import java.util.ResourceBundle; 

/** 
* 国际化资源绑定测试 

* @author leizhimin 2009-7-29 21:17:42 
*/
 
public  class TestResourceBundle { 
         public  static  void main(String[] args) { 
                Locale locale1 =  new Locale( "zh""CN"); 
                ResourceBundle resb1 = ResourceBundle.getBundle( "myres", locale1); 
                System.out.println(resb1.getString( "aaa")); 

                ResourceBundle resb2 = ResourceBundle.getBundle( "myres", Locale.getDefault()); 
                System.out.println(resb1.getString( "aaa")); 

                Locale locale3 =  new Locale( "en""US"); 
                ResourceBundle resb3 = ResourceBundle.getBundle( "myres", locale3); 
                System.out.println(resb3.getString( "aaa")); 
        } 
}
 
运行结果:
好 
好 
good 

Process finished with exit code 0
 
如果使用默认的Locale,那么在英文操作系统上,会选择myres_en_US.properties或myres.properties资源文件。
 
三、认识Locale

Locale 对象表示了特定的地理、政治和文化地区。需要 Locale 来执行其任务的操作称为语言环境敏感的 操作,它使用 Locale 为用户量身定制信息。例如,显示一个数值就是语言环境敏感的操作,应该根据用户的国家、地区或文化的风俗/传统来格式化该数值。
 
使用此类中的构造方法来创建 Locale:
 Locale(String language)
 Locale(String language, String country)
 Locale(String language, String country, String variant)
 
创建完 Locale 后,就可以查询有关其自身的信息。使用 getCountry 可获取 ISO 国家代码,使用 getLanguage 则获取 ISO 语言代码。可用使用 getDisplayCountry 来获取适合向用户显示的国家名。同样,可用使用 getDisplayLanguage 来获取适合向用户显示的语言名。有趣的是,getDisplayXXX 方法本身是语言环境敏感的,它有两个版本:一个使用默认的语言环境作为参数,另一个则使用指定的语言环境作为参数。 
语言参数是一个有效的 ISO 语言代码。这些代码是由 ISO-639 定义的小写两字母代码。在许多网站上都可以找到这些代码的完整列表,如: 
http://www.loc.gov/standards/iso639-2/englangn.html。    
国家参数是一个有效的 ISO 国家代码。这些代码是由 ISO-3166 定义的大写两字母代码。在许多网站上都可以找到这些代码的完整列表,如: 
http://www.iso.ch/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html。    
 
四、中文资源文件的转码 native2ascii
 
这个工具用法如下:
 
如果觉得麻烦,可以直接将中文粘贴到里面,回车就可以看到转码后的结果了。
 
看明白这个了,就不在为struts等web框架的国际化而感到稀奇了。
在java中的应用
static {
/**
* 配置文件变量
*/
ResourceBundle rb = null;
try {
rb = ResourceBundle.getBundle("library");
} catch (Exception e) {
try {
File find = FileFinder.find("library.properties");
if (find != null) {
rb = new PropertyResourceBundle(IOUtil.getReader(find.getAbsolutePath(), System.getProperty("file.encoding")));
LIBRARYLOG.info("load library not find in classPath ! i find it in " + find.getAbsolutePath() + " make sure it is your config!");
}
} catch (Exception e1) {
LIBRARYLOG.warning("not find library.properties. and err " + e.getMessage() + " i think it is a bug!");
}
}

if (rb == null) {
LIBRARYLOG.warning("not find library.properties in classpath use it by default !");
}

if (rb.containsKey("userLibrary"))
userLibrary = rb.getString("userLibrary");
if (rb.containsKey("ambiguityLibrary"))
ambiguityLibrary = rb.getString("ambiguityLibrary");
if (rb.containsKey("isSkipUserDefine"))
isSkipUserDefine = Boolean.valueOf(rb.getString("isSkipUserDefine"));
if (rb.containsKey("isRealName"))
isRealName = Boolean.valueOf(rb.getString("isRealName"));
if (rb.containsKey("crfModel"))
crfModel = rb.getString("crfModel");
}
ObjectInputStream与ObjectOutputStream类

1.

ObjectInputStream与ObjectOutputStream类所读写的对象必须实现Serializable接口,对象中的transient和static类型成员变量不会被读取和写入

2. 

 import java.io.*;

public class serializtion {

 
 public static void main(String[] args)throws IOException{
  Student s1=new Student("张三", 1, 15, "化学");
  Student s2=new Student("李四", 2, 19, "生物");
 
  FileOutputStream fout=new FileOutputStream("student.txt");
  ObjectOutputStream out=new ObjectOutputStream(fout);
  out.writeObject(s1);
  out.writeObject(s2);
  out.close();
  FileInputStream fin=new FileInputStream("student.txt");
  ObjectInputStream in=new ObjectInputStream(fin);
  try {
   s1=(Student) in.readObject();
   s2=(Student) in.readObject();
  } catch (ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
    in.close();
    System.out.print("name:"+s1.name);
    System.out.print(" id:"+s1.id);
    System.out.print(" age:"+s1.age);
    System.out.println(" department:"+s1.department);
    System.out.print("name:"+s2.name);
    System.out.print(" id:"+s2.id);
    System.out.print(" age:"+s2.age);
    System.out.println(" department:"+s2.department);
 }

}
3.import java.io.*;

public class Student implements Serializable {
 
        String name;
        int id ;
     int age;
        String department;

  public Student(String name, int id, int age, String department) {
   this.age = age;
   this.department = department;
   this.id = id;
   this.name = name;

  }
 }




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值