package cn.IO;
import java.io.*;
import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;
public class TongjiTest {
private static final String LINE_SEPARATOR = System
.getProperty("line.separator");
public static void main(String[] args) throws IOException {
Set<Student> set = new TreeSet<Student>(Collections.reverseOrder());
set.add(new Student("李四", 90, 70, 90));
set.add(new Student("王五", 50, 75, 80));
set.add(new Student("赵六", 72, 92, 90));
set.add(new Student("张三", 90, 84, 80));
set.add(new Student("何二", 50, 25, 30));
File dir = new File("f:\\filetest");
if (!dir.exists()) {
dir.mkdir();
}
File destfile = new File(dir, "chengji.text");
writeTOfile(set, destfile);
}
public static void writeTOfile(Set<Student> set, File destfile)
throws IOException {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(destfile);
for (Student stu : set) {
String info = stu.getName() + "\t" + stu.getSum()
+ LINE_SEPARATOR;
fos.write(info.getBytes());
}
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
throw new RuntimeException("系统资源关闭失败");
}
}
}
}
}
|
|