需要class, interface或enum 锘縤mport java.io.*;
import java.io.*;
import java.text.DecimalFormat;
import java.util.*;
public class Test3 {
public static void main(String[] args) {
File in = new File("C:/student.csv");
FileWriter fw = null;
BufferedReader br = null;
if (!in.exists()) {
System.out.println("");
return;
}
try {
br = new BufferedReader(new FileReader(in));
String temp;
List<Student> list = new ArrayList<Student>();
while ((temp = br.readLine()) != null) {
String[] data = temp.split(",");
Student st = new Student();
st.setStudNo(data[0]);
st.setName(data[1]);
st.setMathScore(Double.parseDouble(data[2]));
st.setEnglishScore(Double.parseDouble(data[3]));
st.setComputerScore(Double.parseDouble(data[4]));
list.add(st);
}
br.close();
// 降序排序
Collections.sort(list, Collections.reverseOrder());
fw = new FileWriter("C:/student2.csv");
DecimalFormat df = new DecimalFormat("##0.##");
for (Student s : list) {
fw.write(s.getStudNo());
fw.write(",");
fw.write(s.getName());
fw.write(",");
fw.write(df.format(s.getMathScore()));
fw.write(",");
fw.write(df.format(s.getEnglishScore()));
fw.write(",");
fw.write(df.format(s.getComputerScore()));
fw.write(",");
fw.write(df.format(s.getAvgScore()));
fw.write("\n");
}
System.out.println("output OK");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
if (fw != null) {
fw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class Student{
String Name;
String studno;
Double mathScore;
Double englishScore;
Double computerScore;
Double avgScore;
void setName(String sName){
Name=sName;
}
void setStudNo(String sNo){
studno=sNo;
}
void setMathScore(Double dmathScore){
mathScore=dmathScore;
}
void setEnglishScore(Double denglishScore){
englishScore=denglishScore;
}
void setComputerScore(Double dcomputerScore){
computerScore=dcomputerScore;
}
void setavgScore(){
avgScore=(mathScore+englishScore+computerScore)/3;
}
String getName(){
return Name;
}
String getStudNo(){
return studno;
}
Double getMathScore(){
return mathScore;
}
Double getEnglishScore(){
return englishScore;
}
Double getComputerScore(){
return computerScore;
}
Double getAvgScore(){
return avgScore;
}
}