序列化对象:
复制代码代码如下:
Paket com.chen.seriaizable;
import java.io.Serializable;
java.util.List importieren;
@SuppressWarnings("serial")
öffentliche Klasse Student implementiert Serializable
{
privater String-Name;
private String-ID;
privates Int-Alter;
private List<Student> Students;
öffentlicher String getName()
{
Rückgabename;
}
public void setName(String name)
{
this.name = Name;
}
öffentlicher String getId()
{
Rückgabe-ID;
}
public void setId(String id)
{
this.id = id;
}
public int getAge()
{
Rückkehralter;
}
public void setAge(int age)
{
this.age = Alter;
}
öffentliche Liste<Student> getStudents()
{
Rückkehrstudenten;
}
public void setStudents(List<Student> Students)
{
this.students = Studenten;
}
@Override
öffentlicher String toString()
{
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("id:" + this.id).append("/n");
stringBuffer.append("name:" + this.name).append("/n");
stringBuffer.append("age:" + this.age).append("/n");
return stringBuffer.toString();
}
}
复制代码代码如下:
Paket com.chen.seriaizable;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
java.util.List importieren;
öffentliche Klasse SaveStudent
{
Privatstudent; Student;
// 序列化文件保存位置
private String path = "C:/student.Serializable";
public void writeStudent()
{
List<Student> Students = new ArrayList<Student>();
student = neuer Student();
Student student1 = neuer Student();
student1.setAge(1);
student1.setId("1");
student1.setName("张1");
studenten.add(student1);
Student student2 = neuer Student();
student2.setAge(2);
student2.setId("2");
student2.setName("张2");
studenten.add(student2);
Student student3 = neuer Student();
student3.setAge(3);
student3.setId("3");
student3.setName("张3");
studenten.add(student3);
Student student4 = neuer Student();
student4.setAge(4);
student4.setId("4");
student4.setName("张4");
Student student41 = neuer Student();
student41.setAge(41);
student41.setId("41");
student41.setName("张41");
List<Student> Students4 = new ArrayList<Student>();
studenten4.add(student41);
student4.setStudents(students4);
studenten.add(student4);
student.setAge(500);
student.setId("100");
student.setName("张A000");
student.setStudents(Studenten);
versuchen
{
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(path));
objectOutputStream.writeObject(student);
objectOutputStream.close();
}
Catch (IOException e)
{
// TODO Automatisch generierter Catch-Block
e.printStackTrace();
}
System.out.println("写完");
}
public void readStudent()
{
versuchen
{
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(path));
student = (Student) objectInputStream.readObject();
System.out.println(student.getAge());
objectInputStream.close();
}
Catch (Ausnahme e)
{
e.printStackTrace();
}
System.out.println("frei");
}
@Override
öffentlicher String toString()
{
readStudent();
StringBuffer stringBuffer = new StringBuffer(100);
studentToString(stringBuffer, student);
return stringBuffer.toString();
}
public void studentToString(StringBuffer stringBuffer, Student studentObj)
{
if (Student != null)
{
stringBuffer.append("id:" + studentObj.toString()).append("/n");
if (studentObj.getStudents() != null)
{
stringBuffer.append("/n[/n");
for (Student Bean: studentObj.getStudents())
{
studentToString(stringBuffer, bean);
}
stringBuffer.append("/n]/n");
}
}
}
}
Antwort:
复制代码代码如下:
Paket com.chen.seriaizable;
Öffentlicher Klassentest
{
/**
* @param args
*/
public static void main(String[] args)
{
SaveStudent saveStudent = new SaveStudent();
// 1 先执行写入文件
// saveStudent.writeStudent();
// 2 再读取
System.out.println(saveStudent);
System.out.println("frei");
}
}