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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 孤影卓尔 中级黑马   /  2014-2-24 16:16  /  1158 人查看  /  2 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

有一个txt文件 里面有两列 姓名和年龄 要求读取这个txt文件 按年龄排序 然后新生成一个txt把排序完成后的写在这个txt里 ... 这要怎样完成啊  求大神指点 最好有代码  谢谢了 详细点

评分

参与人数 1技术分 +1 收起 理由
何伟超 + 1

查看全部评分

2 个回复

倒序浏览
1 先建一个student类  有姓名和年龄属性  还要实现comparable接口  compareTo方法按年龄比较
2 主方法建立字符读取流缓冲区 读取txt文件
           按行读去 用split方法切割字符串  new出student对象 加入一个TreeSet集合
           再将集合的元素写入另一个txt文件

评分

参与人数 1技术分 +1 收起 理由
何伟超 + 1

查看全部评分

回复 使用道具 举报
你运行看看:


  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileWriter;
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6. import java.util.Comparator;
  7. import java.util.List;
  8. import java.util.Scanner;

  9. public class $ {

  10.     private static List<Person> data = new ArrayList<Person>();

  11.     public static void main(String... _) throws Exception {

  12.         Scanner in = null;
  13.         try {
  14.             in = new Scanner(new File("D:/a.txt"));

  15.             while (in.hasNextLine()) {
  16.                 String str = in.nextLine();
  17.                 String[] arr = str.split(" ");// 注意txt里面两列之间需有个空格
  18.                 data.add(new Person(arr[0], toInt(arr[1])));
  19.             }
  20.         } catch (FileNotFoundException e) {
  21.             e.printStackTrace();
  22.         } finally {
  23.             if (in != null) {
  24.                 in.close();
  25.             }
  26.         }

  27.         System.out.println("pai xu qian:" + data);

  28.         Collections.sort(data, new Comparator<Person>() {

  29.             public int compare(Person o1, Person o2) {

  30.                 // 按年龄升序
  31.                 return o1.getAge() - o2.getAge();

  32.                 // 按年龄降序
  33.                 // return o2.getAge() - o1.getAge();
  34.             }
  35.         });
  36.         System.out.println("pai xu hou:" + data);

  37.         FileWriter fw = new FileWriter(new File("D:/b.txt"));
  38.         for (Person p : data) {
  39.             fw.write(p.getName() + " " + p.getAge() + "\r\n");
  40.         }
  41.         fw.flush();
  42.         fw.close();
  43.          
  44.         System.out.println("已经保存到D:/b.txt");
  45.     }

  46.     private static int toInt(String str) {
  47.         try {
  48.             return Integer.parseInt(str);
  49.         } catch (Exception e) {
  50.             return 0;
  51.         }
  52.     }
  53. }

  54. class Person {
  55.     private String name;
  56.     private int age;

  57.     public Person(String name, int age) {
  58.         this.name = name;
  59.         this.age = age;
  60.     }

  61.     public String getName() {
  62.         return name;
  63.     }

  64.     public void setName(String name) {
  65.         this.name = name;
  66.     }

  67.     public int getAge() {
  68.         return age;
  69.     }

  70.     public void setAge(int age) {
  71.         this.age = age;
  72.     }

  73.     public String toString() {
  74.         return "{" + name + "," + age + "}";
  75.     }
  76. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
何伟超 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马