A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 209920266 中级黑马   /  2015-1-11 21:51  /  947 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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("系统资源关闭失败");
                                }
                        }
                }
        }
}


1 个回复

倒序浏览
学习下,加油。。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马