java.beans包使用汇总一 (序列化)

写这个是看到一个老兄写的java.beans里某些类的使用方法,但是排版太乱了,小弟在这整理以下,原文地址:http://blog.csdn.net/boboo_2000_0/archive/2008/12/14/3515125.aspx

 

看到java.bean这个包的时候,觉得XMLEncoder和XMLDecoder这两个类有点意思。前者可以直接将JavaBean输入为XML文件,后者可以直接将格式正确的XML文件还原为JavaBean的对象,感觉和对象序列化有点类似,只不过这种方式我们可以清晰地看到转换为普通文件以后的对象的格式,这种方式也可以实现对一个游离对象的持久化。

 

一.先定义两个普通的JavaBean

代码如下

 

package com.bean.test; import java.io.Serializable; public class Role implements Serializable { private static final long serialVersionUID = -40359605540725572L; private int id; private String roleName; private String roleCode; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getRoleCode() { return roleCode; } public void setRoleCode(String roleCode) { this.roleCode = roleCode; } public String getRoleName() { return roleName; } public void setRoleName(String roleName) { this.roleName = roleName; } }

 

另一个javabean类如下

 

package com.bean.test; import java.io.Serializable; import java.util.Date; import java.util.List; public class User implements Serializable { private static final long serialVersionUID = 5076428480402933567L; private String id; private String name; private String password; private int age; private Integer score; private Date birth; private List list; public List getList() { return list; } public void setList(List list) { this.list = list; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Integer getScore() { return score; } public void setScore(Integer score) { this.score = score; } }

 

二. Encoder代码即序列化部分

代码如下,执行后在根目录生成一个xml文件user.xml

进一步验证发现,如果JavaBean的属性的值为默认初始值,那么对应的属性和值将不会输出到输出流。例如,当int=0,String=null,boolean=false,自定义类型=null的时候,将会忽略该节点。注意:String如果为空字符串仍然会输出该节点,同时在从XML文件加载到JavaBean对象的时候,也会根据这些默认值对JavaBean进行填充。

 

package com.bean.test; import java.beans.XMLEncoder; import java.io.BufferedOutputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.Date; import java.util.List; public class DudanBeanToXMLEncoder { public static void main(String[] args) { FileOutputStream fos = null; BufferedOutputStream bos = null; try { fos = new FileOutputStream("user.xml"); } catch (FileNotFoundException e) { e.printStackTrace(); } bos = new BufferedOutputStream(fos); XMLEncoder encoder = new XMLEncoder(bos); User user = new User(); user.setAge(10); user.setBirth(new Date()); user.setId("0x4535435"); user.setName("杜丹"); user.setPassword("111111"); user.setScore(98); List list = new ArrayList(); Role role = null; for (int i = 0; i < 5; i++) { role = new Role(); role.setId(i); role.setRoleName("角色" + i); role.setRoleCode(String.valueOf(10 + i)); list.add(role); } user.setList(list); encoder.writeObject(user); encoder.close(); } }

 

三.以下是从XML文件加载为JavaBean对象的代码

代码如下

package com.bean.test; import java.beans.XMLDecoder; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; public class DudanXMLToBeanDecoder { public static void main(String[] args) { FileInputStream fos = null; BufferedInputStream bos = null; try { fos = new FileInputStream("user.xml"); } catch (FileNotFoundException e) { e.printStackTrace(); } bos = new BufferedInputStream(fos); XMLDecoder encoder = new XMLDecoder(bos); User user = (User) encoder.readObject(); System.out.println(user.getAge()); System.out.println(user.getId()); System.out.println(user.getName()); encoder.close(); } }

你可能感兴趣的:(Date,list,String,user,Integer,Class)