package QQ9_20;
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;
import java.util.ArrayList;
import java.util.List;
// 班级类
class MyClass implements Serializable{
private String cname;
private List<Student> ss;
public MyClass() {
}
public MyClass(String cname, List<Student> ss) {
this.cname = cname;
this.ss = ss;
}
public void setCname(String cname) {
this.cname = cname;
}
public String getCname() {
return cname;
}
public void setSs(List<Student> ss) {
this.ss = ss;
}
public List<Student> getSs() {
return ss;
}
/*public String toString(){
return \t"+this.ss;
}*/
}
// 学生类
class Student extends MyClass implements Serializable{
private int sid;
private String sname;
private int age;
public Student() {
}
public Student(int sid, String sname, int age) {
this.sid = sid;
this.sname = sname;
this.age = age;
}
public void setSid(int sid) {
this.sid = sid;
}
public int getSid() {
return sid;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSname() {
return sname;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
// 显示信息
public String toString() {
return this.getSid() + "\t" + this.getSname() + "\t" + this.getAge();
}
}
public class 班级信息处理 {
static void AddInform() {
// 建立班级对象并建立两个List
List<Student> lis1 = new ArrayList();
List<Student> lis2 = new ArrayList();
// 建立学生类的对象,并且将其传入List中
Student st1 = new Student(23, "陶辉", 15);
Student st2 = new Student(9, "周露康", 13);
Student st3 = new Student(5, "简小华", 13);
Student st4 = new Student(15, "周露霞", 14);
Student st5 = new Student(34, "肖佳鑫", 14);
lis1.add(st1);
lis1.add(st2);
lis2.add(st3);
lis2.add(st4);
lis2.add(st5);
MyClass mc1 = new MyClass("初三(1)班", lis1);
MyClass mc2 = new MyClass("初三(2)班", lis2);
//将班级信息建立List
List<MyClass> mylis = new ArrayList();
mylis.add(mc1);
mylis.add(mc2);
// 将信息写入文件中
/*try {
InputTheFile(mylis);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
// 将信息从文件中读出来
try {
try {
List<MyClass> mylis2 = OutputTheFile();
for (MyClass mc:mylis2){
//输出班级名
System.out.println(mc.getCname());
//输出班级中学生的信息
for(Student ST:mc.getSs()){
System.out.println(ST.toString());
}
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static void InputTheFile(List<MyClass> mylis) throws IOException{
//建立文件与数据的关系 并且将其序列化
FileOutputStream fos = new FileOutputStream("c:\\classinform.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(mylis);
System.out.println("信息成功地写入了文件中!");
oos.close();
fos.close();
}
static List<MyClass> OutputTheFile() throws IOException, ClassNotFoundException
{
FileInputStream fos = new FileInputStream("c:\\classinform.txt");
ObjectInputStream oos = new ObjectInputStream(fos);
List<MyClass> mylis = new ArrayList();
// MyClass MC = new MyClass();
try{
while(true){
MyClass MC = (MyClass)oos.readObject();
mylis.add(MC);
}
}catch(Exception e){
}
finally{
oos.close();
fos.close();
}
System.out.println("文件读取成功!");
return mylis;
}
public static void main(String[] args) {
AddInform();
}
}