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;
}
}
/从一个文件里读数据,然后再传到另外一个文件中,要求进行排序;