【发布时间】:2011-01-27 14:13:27
【问题描述】:
这段代码有一些错误:
错误(18,40):未报告的异常 java.io.FileNotFoundException;必须被抓住或宣布被抛出 错误(19,42):未报告的异常 java.io.IOException;必须被抓住或宣布被抛出
但是当抛出 FileNotFound 和 IOException 异常时,编译器会显示这个错误:
错误(15,27):removeEldestEntry(java.util.Map.Entry) in 无法覆盖 java.util.LinkedHashMap 中的 removeEldestEntry(java.util.Map.Entry);被覆盖的方法不会抛出 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());
}
}
【问题讨论】:
标签: java ioexception filenotfoundexception fileoutputstream