解决方法java.io.EOFException由ObjectInputStream引起

编程入门 行业动态 更新时间:2024-10-25 12:19:51
本文介绍了解决方法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?

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

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

    • 您可以通过检查文件内容的长度是否小于或等于零(表示文件为空)来避免空文件出现EOFException. 另一种检查文件是否为空的方法

    某些代码更改对我有用.

    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引起

    本文发布于:2023-11-04 17:41:09,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1558586.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:解决方法   java   io   EOFException   ObjectInputStream

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!