该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
import java.io.*;
class Person implements Serializable{
private String username;
private int password;
public Person(String username, int password) {
this.username = username;
this.password = password;
}
@Override
public String toString() {
return "Person{" +
"username='" + username + '\'' +
", password=" + password +
'}';
}
}
public class Main {
public static void main(String args[]) throws Exception{
Person per[] = {new Person("qwe",80),new Person("weq",15),new Person("ewq",16)};
MyObjectOutputStream.ser(per);
Object obj[] = dser();
for(int i=0;i
Person p = (Person)obj[i];
System.out.println(p);
}
}
public static class MyObjectOutputStream extends ObjectOutputStream{
public MyObjectOutputStream(OutputStream os) throws IOException {
super(os);
}
protected void writeStreamHeader() throws IOException{
super.reset();
}
public static void ser(Object obj) throws Exception{
File f = new File("D:"+ File.separator+"test.txt");
if(!f.exists()){
f.createNewFile();
}
FileOutputStream fos = new FileOutputStream(f,true);
ObjectOutputStream oos = null;
if(f.length() == 0){
oos = new ObjectOutputStream(fos);
}
else {
System.out.println("111111111111111111");
oos = new MyObjectOutputStream(fos);
}
oos.writeObject(obj);
oos.close();
}
}
public static Object[] dser() throws Exception{
File f = new File("D:"+ File.separator+"test.txt");
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
Object obj[] = (Object[])ois.readObject();
ois.close();
return obj;
}
}