import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class T {
public static void main(String[] args) throws IOException {
String inFile = "D:/1.in";
String outFile = "D:/1.out";
List<Student> students = readStudents(inFile);
Collections.sort(students);
writeStudents(outFile, students);
}
private static void writeStudents(String file, List<Student> students)
throws IOException {
File f = new File(file);
if (f.exists()) {
f.delete();
}
f.createNewFile();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(f), "GBK"));
bw.write("姓名 性别 年龄 业绩得分");
for (Student student : students) {
bw.write(System.lineSeparator());
bw.write(student.toString());
}
bw.close();
}
private static List<Student> readStudents(String file) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(file), "GBK"));
br.readLine();// 舍弃标题行
String line = null;
List<Student> students = new ArrayList<Student>();
while ((line = br.readLine()) != null) {
students.add(parseLine(line));
}
br.close();
return students;
}
private static Student parseLine(String line) {
String[] strs = line.split(" ");
Student student = new Student();
student.name = strs[0];
student.gender = strs[1];
student.age = Integer.parseInt(strs[2]);
student.score = new BigDecimal(strs[3]);
return student;
}
private static class Student implements Comparable<Student> {
public String name;
public String gender;
public int age;
public BigDecimal score;
@Override
public int compareTo(Student o) {
return -score.compareTo(o.score);
}
public String toString() {
return name + " " + gender + " " + age + " " + score.toPlainString();
}
}
}
温馨提示:内容为网友见解,仅供参考