黑马程序员技术交流社区

标题: 这个题目怎么做啊?急! [打印本页]

作者: 孤影卓尔    时间: 2014-2-24 16:16
标题: 这个题目怎么做啊?急!
有一个txt文件 里面有两列 姓名和年龄 要求读取这个txt文件 按年龄排序 然后新生成一个txt把排序完成后的写在这个txt里 ... 这要怎样完成啊  求大神指点 最好有代码  谢谢了 详细点
作者: 长石    时间: 2014-2-24 16:34
1 先建一个student类  有姓名和年龄属性  还要实现comparable接口  compareTo方法按年龄比较
2 主方法建立字符读取流缓冲区 读取txt文件
           按行读去 用split方法切割字符串  new出student对象 加入一个TreeSet集合
           再将集合的元素写入另一个txt文件
作者: 丶小天    时间: 2014-2-24 16:40
你运行看看:


  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. }
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2