多么无力的教训啊(浮躁浮躁浮躁)

import java.io.*;
import java.nio.charset.Charset;
import java.util.*;

/**
* .
*/

public class Main {
public static void main(String[] args) {
//List list = new ArrayList();
List list2 = new ArrayList();
try {
File file = new File(“E:\abc.txt”);
FileInputStream in = new FileInputStream(file);
//两边都要考虑到
InputStreamReader isr = new InputStreamReader(in, Charset.forName(“Unicode”));
BufferedReader br = new BufferedReader(isr);

        String ss = br.readLine();
        while (ss != null) {
            String abc[] = ss.split("\t");
            String a1 = abc[0];
            int a2 = Integer.parseInt(abc[1]);
            Person p = new Person(a1, a2);
            list2.add(p);
         //   list.add(ss);
            ss = br.readLine();
        }
        Collections.sort(list2, new AgeComparator());

// System.out.println(list.size());
// for (int i = 0; i < list.size(); i++) {
// String str1 = list.get(i);
// String abc[] = str1.split(“\t”);
// String a1 = abc[0];
// int a2 = Integer.parseInt(abc[1]);
// Person p = new Person(a1, a2);
// list2.add(p);
// }
// Collections.sort(list2, new AgeComparator());
FileWriter fw = new FileWriter(“D:\test5.txt”);
BufferedWriter bw = new BufferedWriter(fw);

        for (int i = 0; i < list2.size(); i++) {
            bw.write(list2.get(i).name.toString() + "\t" + String.valueOf(list2.get(i).age + "\r\n"));
        }
        bw.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

static class AgeComparator implements Comparator {
    public int compare(Object object1, Object object2) {// 实现接口中的方法
        Person p1 = (Person) object1; // 强制转换
        Person p2 = (Person) object2;

        int age1 = p1.getAge();
        int age2 = p2.getAge();
        if (age1 > age2) {
            return -1;
        } else if (age1 == age2) {
            return 0;
        } else {
            return 1;
        }
    }
}

}

class Person {

public String name;
public int age;

public Person() {
}

public Person(String name, int age) {
    this.name = name;
    this.age = age;
}

public String getName() {
    return name;
}

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

public int getAge() {
    return age;
}

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

}

/从一个文件里读数据,然后再传到另外一个文件中,要求进行排序;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值