欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

序列化,反序列化

程序员文章站 2022-05-08 19:22:18
...

public class SerializeTest implements Serializable {

int member1;
long member2;
String member3;
boolean member4;

public SerializeTest() {
}

public SerializeTest(int m1, long m2, String m3, boolean m4) {
member1 = m1;
member2 = m2;
member3 = m3;
member4 = m4;
}

public byte[] serialize() {
byte[] ret = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try {
dos.writeInt(member1);
dos.writeLong(member2);
dos.writeUTF(member3);
dos.writeBoolean(member4);
ret = baos.toByteArray();
dos.close();
baos.close();
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
public void deserialize(byte[] data) {
ByteArrayInputStream bais = new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bais);
try {
member1 = dis.readInt();
member2 = dis.readLong();
member3 = dis.readUTF();
member4 = dis.readBoolean();

dis.close();
bais.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}


/*
* 把序列化和反序列化封装成一个类Account
*/
import java.io.*;
public class Account {
private String []message;
public Account(){}
public Account(String []message)
{
this.message=message;
}

public void setMessage(String []message)
{
this.message=message;
}
public String[] getMessage()
{
return message;
}

/*
* 序列化方法,把该对象序列化为字节流(数组)以便于RMS存储、管理 *
*/
public byte[] serialize()throws IOException
{
ByteArrayOutputStream byteArray=new ByteArrayOutputStream();
DataOutputStream data=new DataOutputStream(byteArray);
int num=message.length;
data.writeInt(num);//先序列化个数
for(int i=0;i<message.length;i++)
data.writeUTF(message[i]);
byteArray.close();
data.close();
return byteArray.toByteArray();
}

//反序列化
public void deserialize(byte []data)throws IOException
{
ByteArrayInputStream byteArray=new ByteArrayInputStream(data);
DataInputStream dataIN=new DataInputStream(byteArray);
int num=dataIN.readInt();//反序列化得到个数,该个数作为循环次数
message=null
message=new String[num];
for(int i=0;i<num;i++)
message[i]=dataIN.readUTF();
byteArray.close();
dataIN.close();
}
}


[size=x-large][color=red]将对象转换成字节数组[/color][/size]

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class PhoneCard {
private int password;// 电话密码
private long cardNumber;// 电话卡卡号
private double balance;// 电话卡余额
private String conncetNumber;// 电话卡接入号码
private boolean connected;// 电话是否接通

//将对象转换成字节数组
public byte[] PhoneCard2Byte() throws IOException{
byte[] phonecard;// 用于存放转换后的对象
ByteArrayOutputStream baos = new ByteArrayOutputStream();// 字节输出流
DataOutputStream dos = new DataOutputStream(baos);// 数据输出流用于包装字节输出流
dos.writeInt(password);
dos.writeLong(cardNumber);
dos.writeDouble(balance);
dos.writeUTF(conncetNumber);
dos.writeBoolean(connected);
phonecard = baos.toByteArray();// 将写入的数据转换成字节数组
dos.close();
baos.close();
return phonecard;
}
//将字节数组装换成PhoneCard对象
public static PhoneCard byte2PhonCard(byte[] phonecard)throws IOException{
PhoneCard pc = new PhoneCard();
ByteArrayInputStream bais = new ByteArrayInputStream(phonecard);// 字节输入流
DataInputStream dis = new DataInputStream(bais);// 数据输入流用于包装字节输入流
// 从流中读取数据,顺序要和写入时一至
pc.setPassword(dis.readInt());
pc.setCardNumber(dis.readLong());
pc.setBalance(dis.readDouble());
pc.setConncetNumber(dis.readUTF());
pc.setConnected(dis.readBoolean());
dis.close();
bais.close();
return pc;// 返回PhoneCard对象

}

// 返回对象的字符串类型
public String toString(){
return "电话密码 : " + password + " 电话卡号 :" + cardNumber + " 电话余额 :"
+ balance + " 接入号码 :" + conncetNumber + " 接通否 :"
+ connected;
}
public int getPassword() {
return password;
}

public void setPassword(int password) {
this.password = password;
}

public long getCardNumber() {
return cardNumber;
}

public void setCardNumber(long cardNumber) {
this.cardNumber = cardNumber;
}

public double getBalance() {
return balance;
}

public void setBalance(double balance) {
this.balance = balance;
}

public String getConncetNumber() {
return conncetNumber;
}

public void setConncetNumber(String conncetNumber) {
this.conncetNumber = conncetNumber;
}

public boolean isConnected() {
return connected;
}

public void setConnected(boolean connected) {
this.connected = connected;
}

}
相关标签: DOS