# JavaClassroomTest1 **Repository Path**: cjhsyc/java-classroom-test1 ## Basic Information - **Project Name**: JavaClassroomTest1 - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2021-11-25 - **Last Updated**: 2021-11-27 ## Categories & Tags **Categories**: Uncategorized **Tags**: Java ## README ### 一、自定义文件,存放某班级学生期末各科考试原始成绩 1、创建实体类(StudentScore) ``` package entity; import java.io.Serializable; public class StudentScore implements Serializable,Comparable { private Integer studentID; private Integer java;//java成绩 private Integer math;//数学成绩 public StudentScore(Integer studentID, Integer java, Integer math) { this.studentID = studentID; this.java = java; this.math = math; } public Integer getStudentID() { return studentID; } public void setStudentID(Integer studentID) { this.studentID = studentID; } public Integer getJava() { return java; } public void setJava(Integer java) { this.java = java; } public Integer getMath() { return math; } public void setMath(Integer math) { this.math = math; } @Override public String toString() { return "studentScore{" + "studentID=" + studentID + ", java=" + java + ", math=" + math + '}'; } @Override public int compareTo(StudentScore o) {//按java分数比较 return this.java-o.java; } } ``` 2、采用对象输入输出流,写入文件 ``` package test; import entity.StudentScore; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.List; public class Test1 { public static void main(String[] args) throws IOException { ObjectOutputStream obs=new ObjectOutputStream(new FileOutputStream("D:\\StudentScore.txt")); StudentScore s1=new StudentScore(1,90,55); StudentScore s2=new StudentScore(2,33,88); StudentScore s3=new StudentScore(3,67,48); StudentScore s4=new StudentScore(4,92,56); StudentScore s5=new StudentScore(5,78,88); List list=new ArrayList<>(); list.add(s1); list.add(s2); list.add(s3); list.add(s4); list.add(s5); obs.writeObject(list);//写入文件 obs.flush(); obs.close(); } } ``` 3、生成文件(StudentScore.txt),已和源代码一起上传 ### 二、读取上述原始成绩文件,按照某门课程分数高低排序 1、代码如下 ``` package test; import entity.StudentScore; import java.io.FileInputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.util.Collections; import java.util.List; public class Test2 { public static void main(String[] args) throws IOException, ClassNotFoundException { List list=readAndSort(); for(StudentScore s:list){ System.out.println(s); } } public static List readAndSort() throws IOException, ClassNotFoundException { ObjectInputStream ois=new ObjectInputStream(new FileInputStream("D:\\StudentScore.txt")); List list=(List) ois.readObject();//读取文件 ois.close(); Collections.sort(list);//按java分数从小到大排序 return list; } } ``` 2、运行结果如下(按java成绩从小到大排序) ![输入图片说明](https://images.gitee.com/uploads/images/2021/1125/154137_2b7ff117_9634894.png "屏幕截图.png") ### 三、输出排序后的新文件,并在屏幕显示某门课挂科详情。 1、代码如下 ``` package test; import entity.StudentScore; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.util.List; public class Test3 { public static void main(String[] args) throws IOException, ClassNotFoundException { List list= Test2.readAndSort(); ObjectOutputStream obs=new ObjectOutputStream(new FileOutputStream("D:\\NewStudentScore.txt")); obs.writeObject(list);//写入文件 obs.flush(); obs.close(); System.out.println("java挂科详情:"); for(StudentScore s:list){ if(s.getJava()<60){ System.out.println(s); } } System.out.println("数学挂科详情:"); for(StudentScore s:list){ if(s.getMath()<60){ System.out.println(s); } } } } ``` 2、生成文件(NewStudentScore.txt)已和源代码一起上传 3、运行结果如下(各科挂科信息) ![输入图片说明](https://images.gitee.com/uploads/images/2021/1125/154231_c61c2099_9634894.png "屏幕截图.png")