public class SerializableEr implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
}
public class Serial extends SerializableEr {
int id;
String name;
public Serial(int id, String name) {
this.id = id;
this.name = name;
}
public String toString() {
return "DATA: " + id + " " +name;
}
public static void main(String[] args) {
Serial serial=new Serial(1,"4234");
System.out.println("object serial:"+serial);
try{
FileOutputStream fos=new FileOutputStream("serialTest.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(serial);
oos.flush();
oos.close();
}catch(Exception e){
System.out.println("Exception:"+e);
}
}
}
为一个实现Serializable接口的父类,编写一个能够序列化的子类 子类将自动的实现序列化
/**
*
*/
private static final long serialVersionUID = 1L;
}
public class Serial extends SerializableEr {
int id;
String name;
public Serial(int id, String name) {
this.id = id;
this.name = name;
}
public String toString() {
return "DATA: " + id + " " +name;
}
public static void main(String[] args) {
Serial serial=new Serial(1,"4234");
System.out.println("object serial:"+serial);
try{
FileOutputStream fos=new FileOutputStream("serialTest.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(serial);
oos.flush();
oos.close();
}catch(Exception e){
System.out.println("Exception:"+e);
}
}
}
为一个实现Serializable接口的父类,编写一个能够序列化的子类 子类将自动的实现序列化
本文提供了一个Java中实现对象序列化的具体示例,通过创建一个实现了Serializable接口的父类和一个子类来演示如何序列化对象到文件。

被折叠的 条评论
为什么被折叠?



