黑马程序员技术交流社区
标题:
IO流成绩统计
[打印本页]
作者:
209920266
时间:
2015-1-11 21:51
标题:
IO流成绩统计
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("系统资源关闭失败");
}
}
}
}
}
作者:
phil
时间:
2015-1-11 21:54
学习下,加油。。。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2