解决方法java.io.EOFException导致ObjectInputStream

时间:2017-01-13 08:34:58

标签: java oop objectinputstream eofexception

对于我的应用程序,我想使用Map作为数据库。为了保存和加载地图,我使用以下两种方法在database.ser上编写/读取它:

private synchronized void saveDB() {
    try {
        fileOut = new FileOutputStream(db);
        out = new ObjectOutputStream(fileOut);
        out.writeObject(accounts);
        fileOut.close();
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

@SuppressWarnings("unchecked")
private void loadDB() {
    try {
        fileIn = new FileInputStream(db);
        in = new ObjectInputStream(fileIn); // that is where error is produced if fileIn is empty
        accounts = (Map<String, Client>) in.readObject();
        in.close();
        fileIn.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

我想在应用程序启动时加载到Map中,所以我在构造函数中调用这样的方法:

protected DriveatorImpl() {
    accounts = new ConcurrentHashMap<String, Client>();
    db = new File("C:/Users/eduar/git/Multy-Threaded-Bank-System/Bank-Services/database.ser"); 
// also, any suggestions how can I make path to a file more flexible in case I want to run Server side of an app on different machine?
     if (!db.exists()) {
        try {
            db.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
     }
    loadDB(); // loads database when server start 
}

我知道导致错误的原因,但我不知道在设计中应该更改什么以避免ObjectInputStream构造函数接收空流! 关于我能做些什么不同的任何建议?

编辑:我想要注意的是,在新的应用程序中运行database.ser是空的,因为尚未在Map中创建任何条目。

谢谢!

2 个答案:

答案 0 :(得分:0)

摆脱file.exists()/file.createNewFile()垃圾。它正在为你做的就是屏蔽原始FileNotFoundException问题,并变成一个完全可预测的EOFException,因为试图在空流周围构建一个ObjectInputStream。处理原始问题。不要只是移动它,或把它变成别的东西。

答案 1 :(得分:-1)

首先出现EOFExcpetion的原因?

  1. 文件或文件中没有内容为空,您尝试读取文件。

  2. 一些代码更改,它对我有用。

    @SuppressWarnings("unchecked")
    private void loadDB() {
        try {
            if (db.length() <= 0) {
                // if statement evaluates to true even if file doesn't exists
                saveDB(); // save to a file an empty map
                          // if file doesn't exist, it creates a new one 
                          // call loadDB inside constructor
    
            }
            FileInputStream fileIn = new FileInputStream(db);
            ObjectInputStream in = new ObjectInputStream(fileIn); // that is where error is produced if fileIn is empty
            in.readObject();
            in.close();
            fileIn.close();
            System.out.println(accounts);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }