方法一:通过ArrayList集合,将学生对象放进集合,然后通过Collections的sort集合进行自定义排序,最后遍历集合通过字符流将信息写入文件中去,同时通过字符输入流将文件的信息读取打印到控制台。
package Test2;
import java.io.*;
import java.util.*;
public class Demo2 {
public static void main(String[] args) {
ArrayList<Student> treeSet = new ArrayList<>();
//录入成绩
Scanner sc = new Scanner(System.in);
for (int i = 1; i <= 3; i++) {
if (i>=2) {
System.out.println("请输入第"+i+"位学生的姓名");
}
System.out.println("请输入学生姓名:");
String s = sc.next();
System.out.println("请输入语文成绩");
int a = sc.nextInt();
System.out.println("请输入数学成绩");
int b = sc.nextInt();
System.out.println("请输入英语成绩");
int c = sc.nextInt();
//创建学生对象,并将输入的信息初始化入学生对象,存入集合
Student student = new Student();
student.setName(s);
student.setChinese(a);
student.setMath(b);
student.setEnglish(c);
treeSet.add(student);
}
Collections.sort(treeSet, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
int sum1 = o1.getChinese() + o1.getMath() + o1.getEnglish();
int sum2 = o2.getChinese() + o2.getMath() + o2.getEnglish();
return Integer.compare(sum2,sum1);
}
});
try (
FileWriter fw = new FileWriter("random_pencil/src/Test2/abc.txt");
BufferedWriter bw = new BufferedWriter(fw);
) {
for (Student student : treeSet) {
bw.write(student.getName()+",");
bw.write(student.getChinese()+",");
bw.write(student.getMath()+",");
bw.write(student.getEnglish()+"");
bw.newLine();
// bw.write(student.toString());
// bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
try (
FileReader fr = new FileReader("random_pencil/src/Test2/abc.txt");
BufferedReader br=new BufferedReader(fr)
){
String str;
while ((str = br.readLine()) != null) {
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Student{
private String name;
private int chinese;
private int math;
private int english;
public Student() {
}
public Student(String name, int chinese, int math, int english) {
this.name = name;
this.chinese = chinese;
this.math = math;
this.english = english;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
@Override
public String toString() {
return name+","+chinese+","+math+","+english;
}
}
方法二:通过Treeset集合,先自定义好排序规则,将学生对象放进集合,遍历集合通过字符流将信息写入文件中去,同时通过字符输入流将文件的信息读取打印到控制台。
package Test2;
import java.io.*;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;
public class Demo2_1 {
public static void main(String[] args) {
TreeSet<Student1> treeSet = new TreeSet<>(new Comparator<Student1>() {
@Override
public int compare(Student1 o1, Student1 o2) {
int sum1=o1.getChinese()+o1.getMath()+o1.getEnglish();
int sum2=o2.getChinese()+o2.getMath()+o2.getEnglish();
return Integer.compare(sum2,sum1);
}
});
Scanner sc = new Scanner(System.in);
for (int i = 1; i <= 3; i++) {
if (i>=2) {
System.out.println("请输入第"+i+"位学生的姓名");
}
System.out.println("请输入学生姓名:");
String s = sc.next();
System.out.println("请输入语文成绩");
int a = sc.nextInt();
System.out.println("请输入数学成绩");
int b = sc.nextInt();
System.out.println("请输入英语成绩");
int c = sc.nextInt();
//创建学生对象,并将输入的信息初始化入学生对象,存入集合
Student1 student = new Student1();
student.setName(s);
student.setChinese(a);
student.setMath(b);
student.setEnglish(c);
treeSet.add(student);
}
treeSet.forEach(System.out::println);
try(FileWriter fw = new FileWriter("random_pencil/src/Test2/123.txt");
BufferedWriter bw = new BufferedWriter(fw); ){
for (Student1 student1 : treeSet) {
bw.write(student1.getName()+",");
bw.write(student1.getChinese()+",");
bw.write(student1.getMath()+",");
bw.write(student1.getEnglish()+"");
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
try (
FileReader fr = new FileReader("random_pencil/src/Test2/123.txt");
BufferedReader br = new BufferedReader(fr)
) {
String str;
while ((str = br.readLine()) != null) {
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Student1 {
private String name;
private int chinese;
private int math;
private int english;
public Student1() {
}
public Student1(String name, int chinese, int math, int english) {
this.name = name;
this.chinese = chinese;
this.math = math;
this.english = english;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
@Override
public String toString() {
return "Student1{" +
"name='" + name + '\'' +
", chinese=" + chinese +
", math=" + math +
", english=" + english +
'}';
}
}