2011-01-27 258 views
1

有一些錯誤在此代碼:IOException異常和FileNotFoundException異常

Error(18,40): unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown Error(19,42): unreported exception java.io.IOException; must be caught or declared to be thrown

但當FileNotFound和IOException異常拋出異常,編譯器顯示此錯誤:

Error(15,27): removeEldestEntry(java.util.Map.Entry) in cannot override removeEldestEntry(java.util.Map.Entry) in java.util.LinkedHashMap; overridden method does not throw java.io.IOException

請告訴我這個問題? 代碼是在這裏:

package client; 

import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.ObjectOutputStream; 
import java.io.IOException; 
import java.util.*; 


public class level1 { 


private static final int max_cache = 50; 

private Map cache = new LinkedHashMap(max_cache, .75F, true) { 
    protected boolean removeEldestEntry(Map.Entry eldest) { 
     boolean removed = super.removeEldestEntry(eldest); 
     if (removed) { 
      FileOutputStream fos = new FileOutputStream("t.tmp"); 
      ObjectOutputStream oos = new ObjectOutputStream(fos); 

      oos.writeObject(eldest.getValue()); 

      oos.close(); 
     } 
     return removed; 
    } 

}; 


public level1() { 
    for (int i = 1; i < 52; i++) { 
     String string = String.valueOf(i); 
     cache.put(string, string); 
     System.out.println("\rCache size = " + cache.size() + 
          "\tRecent value = " + i + " \tLast value = " + 
          cache.get(string) + "\tValues in cache=" + 
          cache.values()); 

    } 

} 

回答

1

您需要處理FileNotFoundException異常的removeEldestEntry方法內(如手柄,抓住它,並記錄它)。當你重寫一個方法時,你不允許在方法簽名上添加新的異常,因爲那樣你的子類就不再可以替代你的子類。

否則找到一種不同的方式來做到這一點,這樣你的removeEldestEntry將條目排隊並且其他的東西讀取隊列並且執行序列化到文件。實際上,在閱讀Javadoc之後,似乎必須有一個更好的地方來放置這個邏輯,實際執行刪除操作的相同代碼可能更適合做序列化。

1

試試這個..

package client; 

import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.ObjectOutputStream; 
import java.util.LinkedHashMap; 
import java.util.Map; 

public class Level1 { 

    private static final int max_cache = 50; 
    private Map cache = new LinkedHashMap(max_cache, .75F, true) { 

     @Override 
     protected boolean removeEldestEntry(Map.Entry eldest) { 
      boolean removed = super.removeEldestEntry(eldest); 
      if (removed) { 
       FileOutputStream fos; 
       try { 
        fos = new FileOutputStream("t.tmp"); 

        ObjectOutputStream oos = new ObjectOutputStream(fos); 

        oos.writeObject(eldest.getValue()); 

        oos.close(); 

       } catch (IOException ex) { 
        System.err.println("IOException!!"); 
       } catch (FileNotFoundException ex) { 
        System.err.println("FileNotFoundException!!"); 
       } 
      } 
      return removed; 
     } 
    }; 

    public level1() { 
     for (int i = 1; i < 52; i++) { 
      String string = String.valueOf(i); 
      cache.put(string, string); 
      System.out.println("\rCache size = " + cache.size() 
        + "\tRecent value = " + i + " \tLast value = " 
        + cache.get(string) + "\tValues in cache=" 
        + cache.values()); 

     } 

    } 
}