java - IOException 和 FileNotFoundException

标签 java ioexception filenotfoundexception fileoutputstream

这段代码有一些错误:

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

    }

}

最佳答案

试试这个..

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

        }

    }
}

关于java - IOException 和 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4817398/

相关文章:

java - 使用注释处理器创建具有特定注释的类列表

java - 如何强制 Swagger Codegen 生成未从任何其他对象引用的枚举?

c# - 无法从传输连接读取数据 : An existing connection was forcibly closed by the remote host in C#

java - Java (Android) 中的 IOException : Transport endpoint is not connected

java - 文件输出流有问题

java.io.FileNotFoundException : the system cannot find the file specified

c# - AppDomain.Load() 因 FileNotFoundException 而失败

java - 我需要创建一百个 getter 和 setter 吗?

java - 查找重复的表达式/参数

c# - 无法从传输连接读取数据 : An existing connection was forcibly closed by the remote host