序列化参考文献:http://www.cnblogs.com/iOS-mt/p/6600177.html
序列化与反序列化文献:http://blog.csdn.net/wangloveall/article/details/7992448/
注意事项:
类要实现序列化得implements Serializable接口。
并且一定要生成一个序列化的UID
private static final long serialVersionUID = 1L;
如果没有生成,后面反序列化的时候 会出现如下错误:
java.io.InvalidClassException:local class incompatible:stream classdesc serialVersionUID = 29639852904090138, local class serialVersionUID = 3149742550176671398
反序列化时如何判断是否已经读到末尾?
将一组对象序列化到文件中,然后将这些对象用readObject()
方法读取出来,如果使用一个while循环来判断对象不为空的方式读取,最后会因为读取到文件末尾继续读取抛出一个EOFExcetion
。
Object obj = null;
while(null != (obj = ois.readObject()))
{
...
}
序列化中主要是,跟业务相关,不全部列出了:
File fileObject = new File("object");
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileObject));
ProtocolObject object = new ProtocolObject();
然后将要各项属性set后,然后存储
oos.writeObject(object);
oos.flush();
oos.close();
实体类相关代码:
package com.ecnu.mapreduce;
import java.io.Serializable;
public class ProtocolObject implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private Long id;
private String sourceIP;
private String destinationIP;
private Integer sourcePort;
private Integer destinationPort;
private String protocalType;
private String headData;
private String text;
private Boolean isWrong;
@Override
public String toString() {
return "ProtocolObject [id=" + id + ", sourceIP=" + sourceIP + ", destinationIP=" + destinationIP
+ ", sourcePort=" + sourcePort + ", destinationPort=" + destinationPort + ", protocalType="
+ protocalType + ", headData=" + headData + ", text=" + text + ", isWrong=" + isWrong + "]";
}
public ProtocolObject() {
this.isWrong = false;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getSourceIP() {
return sourceIP;
}
public void setSourceIP(String sourceIP) {
this.sourceIP = sourceIP;
}
public String getDestinationIP() {
return destinationIP;
}
public void setDestinationIP(String destinationIP) {
this.destinationIP = destinationIP;
}
public Integer getSourcePort() {
return sourcePort;
}
public void setSourcePort(Integer sourcePort) {
this.sourcePort = sourcePort;
}
public Integer getDestinationPort() {
return destinationPort;
}
public void setDestinationPort(Integer destinationPort) {
this.destinationPort = destinationPort;
}
public String getProtocalType() {
return protocalType;
}
public void setProtocalType(String protocalType) {
this.protocalType = protocalType;
}
public String getHeadData() {
return headData;
}
public void setHeadData(String headData) {
this.headData = headData;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Boolean getIsWrong() {
return isWrong;
}
public void setIsWrong(Boolean isWrong) {
this.isWrong = isWrong;
}
}
反序列化代码:
package com.ecnu.mapreduce;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
public class DeserializeTest {
public static void main(String[] args) throws IOException, ClassNotFoundException {
File file = new File("object");
FileInputStream fis =new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
Object object = null;
while(null!=(object = ois.readObject())) {
ProtocolObject protocol = (ProtocolObject) object;
System.out.println(protocol.toString());
}
fis.close();
ois.close();
}
}
java枚举参考文献:http://blog.csdn.net/qq_27093465/article/details/52180865
自己写的枚举类实例:
public enum Protocol {
QUOTABID("QUOTABID", 0), TCP("TCP", 1), TLSV10("TLSv1.0", 3), TLSV12("TLSv1.2", 4), OTHER("other", 5);
private String name;
private int index;
private Protocol(String name, int index) {
this.name = name;
this.index = index;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
}