java.util.Arrays.sort兩種方式的排序(及檔案讀寫練習)

weixin_34321977發表於2014-07-01
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<index; ++i){
          String str[]=ws[i].split(" ");
          myArray[i] = new myTest(Integer.parseInt(str[0]), str[1]);
          /*
	     開始的時候是這樣寫的, 奧心死了, 作死的節奏啊.....半天沒找出來哪裡出現了空指標
             myArray[i].x=Integer.parseInt(str[0]);
             myArray[i].name=str[1];
 	  */
       }
       //1. 利用 自定義的 Comparator類中的compare 方法進行排序
       Arrays.sort(myArray, 0, index, new myComparator());
       //2. 利用 介面Comparable中的compareTo進行排序
       //Arrays.sort(myArray, 0, index);

       DataOutputStream dOut = new DataOutputStream(new FileOutputStream(new File("SortWords.txt")));
       for(myTest tmp : myArray){
           if(tmp==null) break;
           System.out.println(tmp.x + " " + tmp.name);
           dOut.writeInt(tmp.x);
           dOut.writeChar(' ');
           dOut.writeChars(tmp.name);
           dOut.writeChar('\n');
       }
 
       
       //如果想要利用ObjectIputStream反序列化構造物件,就必須保證原始檔已經是 利用ObjectOutputStream 寫入的,否則出現錯誤
       ObjectOutputStream ObjOut = new ObjectOutputStream(new FileOutputStream(new File("SortWords.txt")));
       for(int i=0; i<index; ++i)
          ObjOut.writeObject(myArray[i]);
       //通過這種方法,可以避免ObjcetInputStream.readObject()中產生EOFException異常
       //ObjOut.writeObject(null);

       index=0;
       myTest inputTmp;
       ObjectInputStream ObjIn = new ObjectInputStream(new FileInputStream(new File("SortWords.txt")));
       try{
         while((inputTmp=(myTest)ObjIn.readObject())!=null){
            myArray[index++]=inputTmp;
         }
       }catch(IOException e){e.printStackTrace(); }finally{ ///放生的EOFException異常時IOException的子類
         System.out.println("EOFException處理完畢!");
       }
       System.out.println("Finish!");
   }
}

class myTest implements Comparable<myTest>, 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<myTest>{
    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;
    }
}

  

相關文章