本文共 1919 字,大约阅读时间需要 6 分钟。
import java.io.*;import java.util.*;public class SortTest{ public static void main(String args[]) throws IOException, ClassNotFoundException { FileReader InWord = new FileReader(new File("words.txt")); BufferedReader in = new BufferedReader(InWord); String ws[] = new String[100]; String input; int index = 0; while((input=in.readLine())!=null) ws[index++]=input; Arrays.sort(ws, 0, index); BufferedWriter out = new BufferedWriter(new FileWriter(new File("SortWords.txt"))); for(String s : ws){ if(s==null) break; System.out.println(s); out.write(s, 0, s.length()); out.newLine(); } in.close(); out.close(); myTest myArray[] = new myTest[20]; in = new BufferedReader(new FileReader(new File("words.txt"))); index=0; while((input=in.readLine())!=null){ ws[index++]=input; } for(int i=0; i, Serializable{ int x; String name; public myTest(){} public myTest(int x, String name){ this.x=x; this.name=name; } public int compareTo(myTest tmp){ if(x==tmp.x) return name.compareTo(tmp.name); else return x-tmp.x; } private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException{ //in.defaultReadObject(); x=(int)in.readInt(); name=(String)in.readObject(); } private void writeObject(ObjectOutputStream out) throws IOException{ //out.defaultWriteObject(); out.writeInt(x); out.writeObject(name); }} class myComparator implements Comparator { public int compare(myTest o1, myTest o2){ if(o1.x==o2.x) return o1.name.compareTo(o2.name); else return o1.x - o2.x; } public boolean equals(Object o1){ return true; }}
转载地址:http://olcia.baihongyu.com/