package lesson0925;
import java.io.Serializable;
public class Student implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private String sex;
private int age;
private double height;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String name, String sex, int age, double height) {
super();
this.name = name;
this.sex = sex;
this.age = age;
this.height = height;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
@Override
public String toString() {
return "Student [name=" + name + ", sex=" + sex + ", age=" + age + ", height=" + height + "]";
}
}
package lesson0925;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class Second {
private static List<Student> list = new ArrayList<Student>();
public static void main(String[] args) throws IOException, ClassNotFoundException {
for(int i=0;i<3;i++) {
Student stu = createStu();
addStu(stu);
}
wrtieObject(list);
list = readObject();
System.out.println(list);
showStu();
showMaxAge();
}
//创建学生
public static Student createStu() {
Scanner scan = new Scanner(System.in);
System.out.println("请输入学生姓名:");
String name = scan.next();
System.out.println("请输入学生年龄:");
int age = scan.nextInt();
System.out.println("请输入学生性别:");
String sex = scan.next();
System.out.println("请输入学生身高:");
double height = scan.nextDouble();
Student stu = new Student(name,sex,age,height);
return stu;
}
//添加5个学生
public static void addStu(Student stu) {
list.add(stu);
}
//添加对象到文件中
public static void wrtieObject(List list) throws IOException {
FileOutputStream fis = new FileOutputStream("student.txt");
ObjectOutputStream oos = new ObjectOutputStream(fis);
oos.writeObject(list);
oos.close();
}
//从文件中读取对象
public static List readObject() throws IOException, ClassNotFoundException {
FileInputStream fis = new FileInputStream("student.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
List list = (List)ois.readObject();
return list;
}
//按照身高倒序输出
public static void showStu() throws ClassNotFoundException, IOException {
list = readObject();
Collections.sort(list, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
return (int)(o2.getHeight()-o1.getHeight());
}
});
System.out.println("排序后:");
System.out.println(list);
}
//显示年龄最大信息
public static void showMaxAge() throws ClassNotFoundException, IOException {
list = readObject();
Student max = list.get(0);
for(Student s:list) {
if(s.getAge()>max.getAge()) {
max = s;
}
}
System.out.println("最大年龄学生的信息:"+max);
}
}