Java Serializable序列化接口

在这里就简单的说一下Serializable接口,主要是在使用Findbugs插件来检查代码bug时,发现大篇幅的报Non-transient non-serializable instance field in serializable class错误。在仔细研究了下后,找到了解决方法,在这里分享一下

结论:
1、如果要可序列化某个类,需要实现Serializable接口
2、为确保序列化与反序列化一致,UID必须不可改变
3、如果不需要序列化成员变量,那么可以将这个变量标为瞬时的,修饰符:transient
4、如果需要序列化成员变量的引用,那么这个引用也需要实Serializable接口



import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
* 本例子是将一个类持久化到硬盘中,然后再反序列化出这个对象。
* 当然如果需要在网络上传输的对象,也需要实现Serializable接口,比如RMI远程方法调用
*/
public class TestSerializable
{
//对象持久化地址
private final static String FILE_PATH = "D://student";

public static void main(String[] args)
{
writeObj();

readObj();
}

//将对象持久化到硬盘
public static void writeObj()
{
ObjectOutputStream oos = null;
FileOutputStream fos = null;
try
{
Student stu1 = new Student(17, "Tom", new Point(108, 230));
System.out.println("stu1: " + stu1);
fos = new FileOutputStream(FILE_PATH);
oos = new ObjectOutputStream(fos);
oos.writeObject(stu1);
oos.flush();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (oos != null)
{
try
{
oos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
if (fos != null)
{
try
{
fos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}

}
}

//反序列持久化对象
public static void readObj()
{
FileInputStream fis = null;
ObjectInputStream ois = null;
try
{
Student stu2 = null;
fis = new FileInputStream(FILE_PATH);
ois = new ObjectInputStream(fis);
Object obj = ois.readObject();
stu2 = (Student) obj;
System.out.println("stu2: " + stu2);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
finally
{
if(ois != null)
{
try
{
ois.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
if(fis != null)
{
try
{
fis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
}

//如果要可序列化某个类,需要实现Serializable接口
class Student implements Serializable
{
//为确保序列化与反序列化一致,UID必须不可改变
private static final long serialVersionUID = 1L;

private String name;

private int age;
//如果不需要序列化成员变量,那么可以将这个变量标为瞬时的,修饰符:transient
//private transient Point point;
//如果需要序列化成员变量的引用,那么这个引用也需要实Serializable接口
private Point point;

public Student(int age, String name, Point point)
{
this.age = age;
this.name = name;
this.point = point;
}

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}

public int getAge()
{
return age;
}

public void setAge(int age)
{
this.age = age;
}

public Point getPoint()
{
return point;
}

public void setPoint(Point point)
{
this.point = point;
}

@Override
public String toString()
{
return "name=" + this.getName() + ", age=" + this.getAge() + ", point{"
+ this.getPoint() + "}";
}

}

class Point implements Serializable
{
private static final long serialVersionUID = 1L;

private int x;
private int y;

public Point(int x, int y)
{
super();
this.x = x;
this.y = y;
}

public int getX()
{
return x;
}

public void setX(int x)
{
this.x = x;
}

public int getY()
{
return y;
}

public void setY(int y)
{
this.y = y;
}

@Override
public String toString()
{
return "x=" + x + ", y=" + y;
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值