首页 > 代码库 > ObjectInputStream与ObjectOutputStream类实现对象的存取
ObjectInputStream与ObjectOutputStream类实现对象的存取
1.
ObjectInputStream与ObjectOutputStream类所读写的对象必须实现Serializable接口,对象中的transient和static类型成员变量不会被读取和写入
2.
Serializable是个对象序列化接口,只有序列化才能实现对象存取
3.
//定义学生类
package job;
import java.io.Serializable;
import java.util.Set;
public class Student implements Serializable{ //注意序列化对象
private static final long serialVersionUID = -7311905363747695209L;
private String name;
private int age;
private int id;
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 int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Student(String name, int age, int id) {
super();
this.name = name;
this.age = age;
this.id = id;
}
public Student() {
super();
// TODO 自动生成的构造函数存根
}
}
//利用动态数组 存取对象
----------------------------------------------------------------------------------------------------------------------------------------------------------
package job;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
public class ObjectStreamDemo {
public static void main(String[] args) throws Exception, ClassNotFoundException {
//建立文件
OutputStream f = new FileOutputStream("a.txt");
//new 两个对象为存储做准备
Student stu = new Student("张三", 16, 1001);
Student stu1 = new Student("李四", 15, 1002);
//构造列表并存入对像
ArrayList<Object> link = new ArrayList<Object>();
link.add(stu);
link.add(stu1);
//将列表转化为对象数组
Object[] stuarr = link.toArray();
//利用对象数组写入文件
ObjectOutputStream oos = new ObjectOutputStream(f);
oos.writeObject(stuarr);
oos.close();
//读取对象
InputStream fi = new FileInputStream("a.txt");
ObjectInputStream ois = new ObjectInputStream(fi);
//读取对象到对象数组
Object[] obj = (Object[]) ois.readObject();
for (int i = 0; i < obj.length; i++) {
Student s = (Student) obj[i];
System.out.print("姓名" + s.getName() + "\t");
System.out.print("年龄" + s.getAge() + "\t");
System.out.println("学好" + s.getId());
}
ois.close();
}
}
4.
另外一种方法 (FileOutputStream(fn, true)追加对象)
package com;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
public class My extends ObjectOutputStream {
public My() throws IOException {
super();
}
public My(OutputStream out) throws IOException {
super(out);
}
@Override
protected void writeStreamHeader() throws IOException {
return;
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------
package com;
import java.io.File;
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.text.ParseException;
import java.text.SimpleDateFormat;
public class ObjectStreamDemo {
public static void main(String[] args) throws FileNotFoundException, IOException, ParseException, ClassNotFoundException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
String fn = "e:/db.dat";
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fn));
while (true) {
try {
Student stu = (Student) ois.readObject();
System.out.println(stu.getName());
System.out.println(stu.getAge());
System.out.println(stu.getBirthday().toLocaleString());
System.out.println("-----------------------------------------");
} catch (Exception e) {
break;
}
}
// 对象输出流,主要将java的对象存储到文件
Student s1 = new Student("李四", 22, sdf.parse("1985年5月20日"));
ObjectOutputStream oos;
if (new File(fn).length() < 1) {
oos = new ObjectOutputStream(new FileOutputStream(fn, true));
} else {
oos = new My(new FileOutputStream(fn, true));
}
oos.writeObject(s1);
oos.close();
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------
ObjectInputStream与ObjectOutputStream类实现对象的存取