1

这段代码有一些错误:

错误(18,40):未报告的异常 java.io.FileNotFoundException;必须被捕获或声明被抛出 Error(19,42): unreported exception java.io.IOException; 必须被抓住或宣布被扔掉

但是当抛出 FileNotFound 和 IOException 异常时,编译器会显示此错误:

错误(15,27):removeEldestEntry(java.util.Map.Entry) in cannot override removeEldestEntry(java.util.Map.Entry) in java.util.LinkedHashMap; 被覆盖的方法不会抛出 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());

    }

}
4

2 回答 2

3

尝试这个..

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());

        }

    }
}
于 2012-07-03T05:44:12.143 回答
1

您需要在您的 removeEldestEntry 方法中处理 FileNotFoundException (按原样处理,捕获并记录它)。当你重写一个方法时,你不能在方法签名上添加新的异常,因为你的子类不再可以替代你子类化的东西。

否则,请找到一种不同的方法来执行此操作,以便您的 removeEldestEntry 将条目排队,而其他东西会读取队列并序列化到文件中。实际上,在阅读 Javadoc 之后,似乎必须有一个更好的地方来放置这个逻辑,实际执行删除的相同代码可能是进行序列化的更好地方。

于 2011-01-27T14:22:23.727 回答