
package com.io.liushuaishuai;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;
public class ArrayListToTxDemo01 {
public static void main(String[] args) throws IOException {
TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int num = s2.sum() - s1.sum();
int num2 = num == 0 ? s2.getChinese() - s1.getChinese() : num;
int num3 = num2 == 0 ? s2.getMath() - s1.getMath() : num2;
int num4 = num3 == 0 ? s2.getName().compareTo(s1.getName()) : num3;
return num4;
}
});
int i;
for (i = 0; i < 5; i++) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第" + (i + 1) + "个学生的信息");
System.out.println("请输入姓名");
String name = sc.nextLine();
System.out.println("请输入语文成绩");
int chinese = sc.nextInt();
System.out.println("请输入数学成绩");
int math = sc.nextInt();
System.out.println("请输入英语成绩");
int english = sc.nextInt();
Student s = new Student(name, chinese, math, english);
ts.add(s);
}
BufferedWriter bw = new BufferedWriter(new FileWriter("myIOstream\\fos.txt"));
for (Student s : ts) {
StringBuilder sb = new StringBuilder();
StringBuilder str = sb.append(s.getName()).append(",").append(s.getChinese()).append(",").append(s.getMath()).append(",").append(s.getEnglish()).append(",").append(s.sum());
String s1 = str.toString();
bw.write(s1);
bw.newLine();
bw.flush();
}
bw.close();
}
}
原文:https://www.cnblogs.com/lsswudi/p/11428731.html