解决方法java.io.EOFException由ObjectInputStream引起 [英] Workaround java.io.EOFException cause by ObjectInputStream

查看:858
本文介绍了解决方法java.io.EOFException由ObjectInputStream引起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的应用程序,我想使用Map充当数据库.为了保存和加载地图,我正在使用以下2种方法将其写入database.ser中/从中读取:

For my application I want to use a Map to act as a database. To save and load a map, I am writing/reading it to/from database.ser using this 2 methods:

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中,所以我在构造函数中调用这样的方法:

I want to load into Map when application starts, so I invoke method in constructor like this:

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构造函数接收空流! 关于我可以做些什么的任何建议?

I am aware of what causing an error, but I don't know what should I change in my design to avoid ObjectInputStream constructor receiving empty stream! Any suggestions on what I can do differently?

我想指出,在新应用程序中运行database.ser为空,因为尚未在Map中进行任何输入.

I want to note that in fresh application run database.ser is empty since there was no entries made into Map yet.

谢谢!

推荐答案

为什么会出现EOFExcpetion?

First why the EOFExcpetion occur?

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

  1. There are no contents in file or file is empty and you tried to read file.

某些代码更改对我有用.

Some code change and it worked for me.

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

这篇关于解决方法java.io.EOFException由ObjectInputStream引起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆