【问题标题】:IOException and FileNotFoundExceptionIOException 和 FileNotFoundException
【发布时间】: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


    【解决方案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());
    
            }
    
        }
    }
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2016-08-18
        • 2018-02-10
        • 2013-09-28
        • 2020-01-15
        • 2013-07-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多