IOException 和 FileNotFoundException
这段代码有一些错误:
错误(18,40):未报告的异常java.io.FileNotFoundException;必须被抓住或宣布被抛出 错误(19,42):未报告的异常java.io.IOException;必须被抓住或宣布被抛出
但是当抛出 FileNotFound 和 IOException 异常时,编译器会显示以下错误:
错误(15,27):removeEldestEntry(java.util.Map.Entry)无法覆盖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());
}
}
Have some errors in this code:
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
But when FileNotFound and IOException exceptions are thrown, compiler shows this error:
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
Whats the problem?
Code is here:
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());
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在removeEldestEntry方法中处理FileNotFoundException(按如下方式处理,捕获它并记录它)。当您重写方法时,不允许您在方法签名上添加新的异常,因为这样您的子类就不再可以替代您正在子类化的事物。
否则,找到一种不同的方法来执行此操作,以便您的removeEldestEntry将条目排队,然后由其他程序读取队列并对文件进行序列化。实际上,在阅读了 Javadoc 后,似乎必须有一个更好的地方来放置此逻辑,实际执行删除的相同代码可能是进行序列化的更好地方。
You need to handle the FileNotFoundException inside your removeEldestEntry method (handle as in, catch it and log it). You're not allowed to tack new exceptions onto the method signature when you override a method, because then your subclass is no longer substitutable for the thing you're subclassing.
Otherwise find a different way to do it so that your removeEldestEntry queues the entries up and something else reads the queue and does the serializing to a file. Actually after reading the Javadoc it seems like there must be a better place to put this logic, the same code that actually performs the removal would probably be a better place to do the serializing.
试试这个..
Try this..