IOException 和 FileNotFoundException

发布于 2024-10-14 17:46:03 字数 1542 浏览 8 评论 0原文

这段代码有一些错误:

错误(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一个人的旅程 2024-10-21 17:46:04

您需要在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.

冰雪梦之恋 2024-10-21 17:46:03

试试这个..

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

        }

    }
}

Try this..

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

        }

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文